1

Host-Named Site Collections (Provisioning by Code and in Specific Content Database)

Host-named site collections are the preferred method to deploy sites in SharePoint 2013.

Because the Office 365 environment uses host-named site collections, new features are optimized for these site collections and they are expected to be more reliable.

Creating Host Named site collection in a on premise environment still needs to be done with the use of PowerShell.

New-SPSite 'http://dev.sharepoint.local' -HostHeaderWebApplication 'http://webapplication.sharepoint.local’ -Name 'Host-Named Site Collection' -Description 'My First Host-Name Site Collection' -OwnerAlias 'SharePoint\spadmin' -language 1033 -Template 'STS#0'

When you would like to create the specific site collection in a separate content database you could use the following command.

New-SPSite 'http://dev.sharepoint.local' -HostHeaderWebApplication 'http://webapplication.sharepoint.local’ -Name 'Host-Named Site Collection' -Description 'My First Host-Name Site Collection' -OwnerAlias 'SharePoint\spadmin' -language 1033 -Template 'STS#0' -ContentDatabase 'SP2013_Content_HostNamed'

For more information on the out of the box functionalities:

If you work in a environment were site need to be provisioned  you will have to create a solution to create host-named site collections.

But how do you create host-named site collection by using code (C#).

At first you will have to get a reference to the web application were in you would like to create the host-named site collection. After that you can just use the ‘Add’ method on the ‘Sites’ collection.

SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://webapplication.sharepoint.local"));
SPSite Site = webApp.Sites.Add("http://dev.sharepoint.local", "Host-Named Site Collection","My First Host-Name Site Collection", 1033, "STS#0", "SharePoint\spadmin", "SharePoint Administrator", "spadmin@sharepoint.local", "SharePoint\poweruser”,"Power User", "poweruser@sharepoint.local", true);

But what should you do if you would like to create a host-named site collection in a specific content database in C#.

bool retVal = false;
SPWebApplication webApp = SPWebApplication.Lookup(new Uri(hostheaderApplication));
SPContentDatabase newDatabase = null;

if (webApp != null) {

    bool continueValue = false;
    if (!string.IsNullOrEmpty(contentDatabase)) {
        //check if database exists
        var query = from SPContentDatabase database in webApp.ContentDatabases
                    where database.Name == contentDatabase
                    select database;

        newDatabase = query.FirstOrDefault();
        continueValue = newDatabase == null ? false : true;
    } else {
        continueValue = true;
    }

    if (continueValue) {
        SPSite createdSite = webApp.Sites.Add(url, name, description, languageId, template, ownerAlias, string.Empty, string.Empty, ownerAlias, string.Empty, string.Empty, true);

        SPContentDatabase database = createdSite.ContentDatabase;
        if (database.Name != contentDatabase) {
            Dictionary<SPSite, string> failed = new Dictionary<SPSite, string>();

            database.Move(newDatabase, new List<SPSite>() { createdSite }, out failed);

            if (failed.Count > 0) {
                createdSite.Delete();
            } else {
                retVal = true;
            }
        } else {
            retVal = true;
        }
    }
}

The above code first checks if the content database exists. If it does not exist the code will return a return value of false. When it does exists, it will create the site collection. After the site collection is created it checks if it is created in the right content database if it is not created in the right content database it will move it to the right one.

Related Posts

Forcing a Device Channel Device channels are a way of using different master pages for different devices. MSDN describes the functionality as followed:   “Browsing ...
Starting a Workflow with CSOM There are situations were you would like to start a workflow by using  code. In one of my last projects there was that kind of situation, in that proj...
Offline Installation SharePoint 2013 In many situation you would like to do a offline installation of SharePoint 2013 or you are required to do a offline installation because you do not h...
User automatically removed from SharePoint Group During my current project we received an access request from a user. We did what we normally do, we checked the request and added the user in the appr...
Unable to start User Profile Synchronization Service A few days ago I wrote a post about loading modules in PowerShell by default: “Load modules by default when opening PowerShell”. Today we had to setup...
Filtering SharePoint Search Results by Content Source Working for one my projects we were looking for a solution to only retrieve search results for a specific Content Source. In our scenario we had a Ext...

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.