U2U Blog

for developers and other creative minds

TextBox

An Office 365 App for Site Collection Provisioning in SharePoint Online

Continuing on my previous post, I decided to create a new Office 365 app that would be able to manage site collections in SharePoint Online.

When deciding to build an app that does site collection provisioning, you could chose to do this as a SharePoint App, but SharePoint Apps are typically contextual to the web they're hosted in. With the introduction of Office 365 Apps, which are contextual to the Office 365 tenant, provisioning site collections from an app seems more to be a task for an Office 365 App.

My new application should be capable of:

  • Getting an overview of available site collections
  • Creating a new site collection
  • Delete an existing site collection

My previous post proved that you can access the SharePoint Online sites by means of the Client Side Object Model (CSOM). My goal now is to prove that you can also do SharePoint online administration tasks from an Office 365 app, using also CSOM.

If you’re interested in the full source code, you can get it from GitHub. There you’ll also find how you can register the app in Azure Active Directory.

Accessing SharePoint Online Administration

As an extension to the SharePoint Client Side Object Model, you have the “Microsoft.Online.SharePoint.Client.Tenant.dll” library which gives you access to SharePoint Online Administration tasks. I.e. creating/deleting site collections, managing deleted site collections, … .

Now, would it then be possible to use the Azure application registration and authentication to i.e. manage site collections? Yes, it is possible!
In my previous post, I mentioned how you obtain the access token from Azure Active Directory. You can then get an accessToken for SharePoint Online administration as follows:

string clientID = ConfigurationManager.AppSettings["ida:ClientId"] ?? ConfigurationManager.AppSettings["ida:ClientID"];
string appKey = ConfigurationManager.AppSettings["ida:AppKey"] ?? ConfigurationManager.AppSettings["ida:Password"];
string spSiteUrl = "https://tenant-admin.sharepoint.com";

string accessToken = await GetAccessToken(clientID, appKey, spSiteUrl)

Take care that you replace tenant by your tenant name. Now that you have the accesToken, you can create the ClientContext as follows:

ClientContext clientContext = new ClientContext("https://tenant-admin.sharepoint.com");
clientContext.ExecutingWebRequest +=
    (sender, e) =>
    {
        e.WebRequestExecutor.WebRequest.Headers["Authorization"] = "Bearer " + accessToken;
    };

Getting an overview of all the site collections

Now that you have the ClientContext, you can start using the Tenant class to get an overview of the available site collections. You can do this as follows:

// Create tenant
Tenant tenant = new Tenant(ctx);

// Get the site properties
SPOSitePropertiesEnumerable allSiteProperties = tenant.GetSiteProperties(0, true);

// Load
ctx.Load(allSiteProperties);
await ctx.ExecuteQueryAsync();

The GetSiteProperties method currently only returns a maximum of 300 site collections. If you would like to get the next 300, increment the startIndex parameter to 300.

Creating a new site collection

In order to create a new site collection, you need to know the code of the webtemplate that should be applied to the new site collection. Now if you know the template code by hart, you can just pass it as a string, but better is to first check the available templates in SharePoint Online and chose the one you want to use. Because you have a ClientContext to your SharePoint Online Administration, you can check the web property for available web templates. You can do this as follows:

// Get the webtemplates
WebTemplateCollection webTemplates = ctx.Web.GetAvailableWebTemplates(lcid, false);

// Load
ctx.Load(webTemplates, templates => templates.Where(t => t.IsHidden == false));

// Execute
await ctx.ExecuteQueryAsync();

The GetAvailableWebTemplates method takes in the language code lcid. Here also, if you know the language code that is available by hart, you can just pass it in here. Another approach would be to first check which languages are available in SharePoint Online:

// Load
ctx.Load(ctx.Web, w => w.SupportedUILanguageIds);

// Execute query
await ctx.ExecuteQueryAsync();

Now that we have the ClientContext, Language and template; you can create a new site collection. The Tenant class provides a method called CreateSite that accepts a SiteCreationProperties object as input and creates a new site collection using the properties supplied in the SiteCreationProperties instance.

The snippet below illustrates how you can create a new site collection. In this snippet, you might observe that the CreateSite method returns an SpoOperation. Now, any operation that we can do on a site collection will return an SpoOperation. So when we create a new site collection, it will return an SpoOperation containing the information about the site collection creation operation. You can check the IsComplete property on the SpoOperation to check whether the operation has completed or not (reload the SpoOperation to check again), this is because the creation of a site collection happens asynchronously.

// Create tenant
Tenant tenant = new Tenant(ctx);

// Create properties
SiteCreationProperties siteCreationProperties =
    new SiteCreationProperties()
    {
        Url = "https://tenant.sharepoint.com/sites/createdbycode"
        ,
        Title = "Site created by code"
        ,
        Owner = "admin@tenant.onmicrosoft.com"
        ,
        Template = "STS#0"
        ,
        StorageMaximumLevel = 100
        ,
        UserCodeMaximumLevel = 50
    };

// Create the sitecollection
SpoOperation operation = tenant.CreateSite(siteCreationProperties);

// Execute query
ctx.ExecuteQuery();

Deleting an existing site collection

In order to delete an existing site collection, the basic steps are again the same. The difference is the method that will be called from the Tenant class, being RemoveSite. Do note that the site collection is not permanently deleted, but is pushed to the recycle bin in which it can still be restored. Note that the RemoveSite also returns an SpoOperation.

// Create tenant
Tenant tenant = new Tenant(ctx);

// Perform delete
SpoOperation spoOperation = tenant.RemoveSite("https://tenant.sharepoint.com/sites/createdbycode");

// Load and execute
ctx.Load(spoOperation);
await ctx.ExecuteQueryAsync();

App screenshots

Overview of sites:

05

Creating a new site:

06

Comments (45) -

  • Maxie Shahin

    6/11/2015 3:31:59 PM | Reply

    you have nice blog can you allow me to share your post to mfb fanpage here > fb.com/viralberg or viralberg,, if you dont mind i can linkback to you

  • commercial paving

    6/14/2015 10:43:40 AM | Reply

    Thanks-a-mundo for the post.Really thank you! Really Great.

  • rental mobil di manado

    6/14/2015 5:14:10 PM | Reply

    Thanks for sharing, this is a fantastic blog post.Much thanks again. Will read on...

  • miracolo per acufeni pdf

    6/15/2015 2:41:52 PM | Reply

    Awesome blog.Much thanks again. Want more.

  • libera dalla cellulite scaricare

    6/16/2015 1:09:16 AM | Reply

    Great blog. Keep writing.

  • Travel Choice

    6/17/2015 3:33:40 PM | Reply

    Really appreciate you sharing this post.Thanks Again. Cool.

  • seo backlinks

    6/18/2015 2:11:52 PM | Reply

    Wow! Simply outstanding! Thanks so much for the information discussed! I recognize you have actually placed a lot of effort into this as well as I would like to tell you just how grateful I am! There have to be much more blog sites such as this on the internet! I will certainly subscribe and bookmark to your exceptional website! Hope you produced even more incredible stuff in the soon and also I will certainly come back as well as review it! Keep up the magnum opus!

  • backlinks free

    6/18/2015 10:07:14 PM | Reply

    Not just high quality, however likewise valuable details. And that is rare to come by these days! I have to claim that I am truly amazed and also will definitely come back again if you maintain up the quality and worth of the material at this degree, or even get it on the next degree.

  • ecommerce web development

    6/23/2015 3:33:25 AM | Reply

    I really like and appreciate your article.Really thank you! Great.

  • Data in June 2015

    6/23/2015 12:56:25 PM | Reply

    Very good blog article.Really looking forward to read more. Cool.

  • anal orgy

    6/24/2015 5:04:26 AM | Reply

    Great post.Much thanks again. Really Cool.

  • Napoleon Morrish

    6/24/2015 10:11:36 AM | Reply

    Wordpress blog posts being emailed to another persons blogs subscribers?

  • Boxing June

    6/24/2015 1:21:39 PM | Reply

    Im thankful for the blog. Fantastic.

  • loona lux

    6/25/2015 1:10:55 PM | Reply

    Really informative blog post.Really thank you! Fantastic.

  • Lanita Angilletta

    6/26/2015 12:32:13 AM | Reply

    Hi.  I'm looking for a good site that offers creative writing prompts or creative writing exercises since I heard that anyone who is serious about writing should do creative writing exercises.  So does anyone know of a good one?.

  • backlinks google

    6/26/2015 2:34:22 AM | Reply

    Wow! Just amazing! Thanks a lot for the details shared! I know you have put a bunch of initiative right into this as well as I wanted to inform you exactly how grateful I am! There need to be more blog sites such as this on the net! I will absolutely bookmark as well as subscribe to your impressive site! Hope you produced even more remarkable things in the soon as well as I will certainly come back and also review it! Maintain the great work!

  • link building in 2015

    6/26/2015 4:56:57 AM | Reply

    Not simply quality, however also important information. And also that is uncommon to come by these days! I have to say that I am actually amazed as well as will undoubtedly come back again if you maintain up the reputable as well as worth of the content at this degree, or even get it on the following level.

  • Mobil telefonbog

    6/26/2015 4:40:42 PM | Reply

    This is one awesome blog post. Want more.

  • vfstreaminggratuit.net

    6/29/2015 1:17:46 AM | Reply

    Really informative post.

  • investigation

    6/29/2015 3:19:49 AM | Reply

    I really enjoy the article.Really looking forward to read more. Great.

  • annuaire inverse

    6/29/2015 6:42:30 AM | Reply

    This is one awesome blog post. Want more.

  • June Post

    6/29/2015 8:56:26 AM | Reply

    Im thankful for the article post.Thanks Again. Much obliged.

  • w88 casino

    6/29/2015 2:13:17 PM | Reply

    Wohh precisely what I was searching  for, thankyou for putting up.

  • messi signed shirt

    6/29/2015 3:14:24 PM | Reply

    What as Happening i am new to this, I stumbled upon this I have found It absolutely useful and it has helped me out loads. I hope to contribute & help other users like its helped me. Good job.

  • My Camp 2015

    6/30/2015 4:43:52 AM | Reply

    Say, you got a nice blog article.Much thanks again. Cool.

  • Roofing Contractor Plano

    6/30/2015 12:45:32 PM | Reply

    I truly appreciate this post. I ave been looking everywhere for this! Thank goodness I found it on Bing. You ave made my day! Thanks again.

  • solar modules

    7/1/2015 2:26:18 AM | Reply

    I really liked your blog post. Really Cool.

  • high pr backlinks service

    7/6/2015 4:40:04 AM | Reply

    Not simply reputable, however additionally valuable information. And that is uncommon to come by these days! I have to claim that I am truly satisfied and also will surely come back once again if you maintain up the quality as well as worth of the content at this degree, or even get it on the following degree.

  • backlinks 2015

    7/6/2015 10:32:14 AM | Reply

    Outstanding! Merely all the great words use to this material! Thank you from the bottom of my heart as well as heart and I will come browse through once more if you maintain up the excellent job!

  • blog here

    7/20/2015 12:53:50 AM | Reply

    I simply want to say I am new to blogging and absolutely liked this web page. More than likely I’m want to bookmark your blog . You certainly come with fabulous writings. Appreciate it for sharing with us your web-site.

  • eebest8 mmk

    7/20/2015 11:09:50 PM | Reply

    Wow! This could be one particular of the most helpful blogs We have ever arrive across on this subject. Actually Excellent. I am also an expert in this topic so I can understand your effort.

  • may loc nuoc loai tot

    7/30/2015 1:41:24 AM | Reply

    +Simply, admirable what you have done here . It is the view you express from the heart heart and your clarity on this important content can be easily looked . In theory I want to write like this also - spending time and real effort to make a good article ... but what can I say ... I delayed a lot and never seem to get something done<p><a href="http://nuocchat.com";>may loc nuoc</a></p>

  • may loc nuoc cua nga

    8/1/2015 12:20:19 PM | Reply

    +This article amazing for me, I will visit your site more.thanks <p><a href="http://nuocchat.com";>may loc nuoc</a></p>

  • eebest8

    8/15/2015 11:03:49 AM | Reply

    Very neat article post.Really looking forward to read more. Cool.

  • click resources

    9/7/2015 10:52:09 PM | Reply

    I simply want to tell you that I am just newbie to blogging and definitely enjoyed you're page. Probably I’m going to bookmark your blog post . You actually come with remarkable stories. Thanks for sharing with us your webpage.

  • Joesph Grafton

    9/21/2015 2:49:28 AM | Reply

    This article is very fabulous, just what i was looking for to check out on my time out. Thanks

Loading

TextBox