Creating a host-named site collection programmatically

A host header is a third piece of information that you can use in addition to the IP address and port number to uniquely identify a Web domain or, as Microsoft calls it, an application server.

In SharePoint 2010, You can apply host headers at two different levels :

  • The Web application (IIS Web site) level
  • The site collection level

The following code snippet programmatically creates the host-named site collection with the URL https://training.sharepoint.com in the SharePoint Server 2010 Web application with the URL https://sharepoint

SPWebApplication webApp = SPWebApplication.Lookup(new Uri("https://sharepoint"));
SPSiteCollection sites = webApp.Sites;
SPSite Site = sites.Add(“https://training.sharepoint.com”, "Training","Training Site", 1025, "STS#0", "domain\ayman",
"Ayman El-Hattab", “ayman@aymanelhattab.com”, "domain\marwan”,"Marwan Tarek", "marwan@marwantarek.net", true);

You can also use PowerShell to achieve the same results, for more info refer to : https://technet.microsoft.com/en-us/library/cc424952.aspx

Comments

  • Anonymous
    January 01, 2003
    Hi Ayman,

    I am trying to create content database through C#.
    Kindly tell which class to use create content database.

    Regards,
    Gaurav
  • Anonymous
    August 28, 2013
    Hi, I want to create a new site collection named " training.sharepoint.com/.../IT" Kindly guide how to acheiev this via c# code.