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.

One Reply to “Host-Named Site Collections (Provisioning by Code and in Specific Content Database)”

Leave a Reply

Your email address will not be published.

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