Multi Tenancy in SharePoint 2010 Part 2

In this posting I wanted to come back to the topic of multi tenancy. In a previous post (https://blogs.technet.com/speschka/archive/2009/11/30/enabling-multi-tenant-support-in-sharepoint-2010.aspx) I described some of the basic features of multi tenancy and a) how to create the basic service application infrastructure needed to support multi tenancy and b) how to create a “multi tenant aware” service application instance. I didn’t really go into how to create a new subscription, a multi tenant admin site, or add sites to a subscription. I’ll cover that in this post, and then in part 3 of multi tenancy (at a later date) I’ll cover the concepts of feature packs – how you create and manage them.

 

To begin with, let’s cover how one creates a new subscription; the ID associated with a subscription is how we associate all the sites, data and features together. In PowerShell you can create a new subscription like this:

 

$sub = New-SPSiteSubscription

 

The “$sub” is not really necessary, but it’s useful because then you can use it in other PowerShell commands to continue working with the subscription. All you’ve got at this point is a subscription and a GUID to identify it – there aren’t any sites associated with the subscription. If you want to create a new site and associated it with the subscription, you can do so like this:

 

New-SPSite –Url https://yourNewSite -OwnerAlias myDomain\myUser -OwnerEmail myemail@mydomain.com -Template STS#0 -SiteSubscription $sub

 

So in this example I’ve just reused the $sub variable from the previous command, created a new site, and it is then associated with that subscription. Using the object model, I can do it like this:

 

//get the site to make sure it's valid

SPSite newSite = new SPSite("https://yourNewSite");

 

//create a new subscription

SPSiteSubscription sub = SPSiteSubscription.Create();

 

//add the site to the subscription

sub.Add(newSite);

 

So now I’ve got a subscription and I’ve got a site that is part of that subscription. I probably also want to create the administration site for that subscription. In beta 2 it is a two step process; by RTM it will hopefully be one. The first step is to create a new site and configure it to use the tenant admin site template:

 

$admin = New-SPSite –Url https://yourAdminSite -OwnerAlias myDomain\myUser -OwnerEmail myemail@mydomain.com -Template tenantadmin#0 -SiteSubscription $sub

 

Now that you have the admin site created, you need to set the AdministrationSiteType property, using the SPAdministrationSiteType enum. In PowerShell it would look like this:

$admin.AdministrationSiteType = [Microsoft.SharePoint.SPAdministrationSiteType]::TenantAdministration

 

When the command completes it echos back the new AdministrationSiteType value for the site. By RTM we’re hoping that the New-SPSite cmdlet will include a parameter called –AdministrationType and you would just pass in the value of TenantAdministration.

 

If you were doing this via the object model, the code would be very similar to what I showed above. The only differences would be a) you would want to make sure you use the tenantadmin#0 site template for the new site and b) after the site is created you need to set the AdministrationSiteType property to SPAdministrationSiteType.TenantAdministration.

 

Finally, to add an existing site you need to get the site, then add it to the subscription. Here is an example:

 

$newSite = Get-SPSite –identity https://urlToYourSite

$sub.Add($newSite)

 

That’s all there is too it. Using the object model is very similar, as you would hope. You just need to get a reference to an SPSite object, then a reference to an SPSiteSubscription, then Add the site. Here is an example (NOTE: yes, I am not showing the Dispose and all that stuff but please use best practices in your code):

 

//get the site to make sure it's valid

SPSite newSite = new SPSite("https://urlToYourSite");

 

//get the subscription

SPSiteSubscription sub = theFarm.SiteSubscriptions[new Guid(yourGuidHere)];

 

//add the site to the subscription

sub.Add(newSite);

 

You should be well on your way now to getting your multi tenancy features up and running. Next time we’ll look at feature packs.