SharePoint Site Provisioning with SharePoint Object Model

 

The below code can be re-used as is for creating any number of Site Collections and Sub sites.

The code is provided for two of the scenarios:

Case 1: Creating Site Collection and Sub Sites Inside It

Case 2: Adding Sub Site to already existing Site Collection/Sub Site

 

Step 1: Microsoft.SharePoint DLL should be added in the References Folder

 

 

image

 

Step 2:

Add the reference of below Classes:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

 

Step 3:

Case 1: Code to Create Site Collection and Sub Sites inside it.

Any number of Site Collection and Sub sites can be created on putting the below code in a loop.

The below sample code will create one site collection and one sub site inside it.

 

 try 
 { 
 
 SPSecurity.RunWithElevatedPrivileges( 
 delegate 
 { 
 // Provide the Web Application URL under which all site collection and sub sites needs to be provisioned 
 string webApplicationUrl = "https://hpsweb:3030/"; 
 
 /* --------------------------------------------------------------------------------------- */ 
 // For Creating HR Site Collection 
 
 string relativeUrl = "/sites/hr"; 
 string siteTitle = "HR Site"; 
 string siteDescription = "WElcome to HR Site Collection"; 
 uint languageCode = 1033; // 1033 is the code for english 
 string siteTemplate = "BLANKINTERNET#0"; 
 string primaryAdmin = @"Hpsintegration\Administrator"; // Domain followed by Network Id/Alias 
 string secondaryAdmin = @"Hpsintegration\Administrator2"; //Domain followed by Network Id/Alias. If Secondary Admin is not required then assign Null 
 
 /* --------------------------------------------------------------------------------------- */ 
 
 
 SPWebApplication objWebApplication = SPWebApplication.Lookup(new Uri(webApplicationUrl)); 
 
 using (SPSite mySiteCollection = objWebApplication.Sites.Add(relativeUrl, siteTitle, siteDescription, languageCode, siteTemplate, primaryAdmin, string.Empty, string.Empty, secondaryAdmin, string.Empty, string.Empty)) 
 { 
 /* --------------------------------------------------------------------------------------- */ 
 // For Creating Benifits Sub Site inside HR Site Collection 
 
 string subSiteTitle = "Benifits"; 
 string subSiteDescription = "Employee Benifits"; 
 string subSiteRelativeUrl = "/sites/hr/benifits"; // Benifits Sub Site will get added to the HR site collection 
 
 /* --------------------------------------------------------------------------------------- */ 
 
 using (SPWeb web = mySiteCollection.AllWebs.Add(subSiteRelativeUrl, subSiteTitle, subSiteDescription, languageCode, siteTemplate, false, false)) ; 
 
 } 
 }); 
 } 
 catch (Exception ex) 
 { 
 }

 

 

Step 3:

Case 2: Code to Create a Site inside a Site Collection

Any number of Sub sites can be added by putting the below code in a loop.

The below sample code will create one sub site inside a site collection. Pre requisite: Site Collection should exist under which sub site needs to be added.

  try 
 { 
 
 SPSecurity.RunWithElevatedPrivileges( 
 delegate 
 { 
 string webApplicationUrl = "https://hpsweb:3030/"; // Web Application URL 
 string parentSiteUrl = "/sites/hr"; // URL under which to create sub site 
 
 string requestSiteUrl = "/sites/hr/compensation"; // New sub site to add 
 string siteTitle = "Compensation"; 
 string siteDescription = "Compensation Details"; 
 uint languageCode = 1033; // 1033 is the code for english 
 string siteTemplate = "BLANKINTERNET#0"; 
 
 parentSiteUrl = parentSiteUrl.TrimStart('/'); 
 string requestUrl = String.Concat(webApplicationUrl, parentSiteUrl); 
 
 using (SPSite objSiteCollection = new SPSite(requestUrl)) 
 { 
 using (SPWeb web = objSiteCollection.AllWebs.Add(requestSiteUrl, siteTitle, siteDescription, languageCode, siteTemplate, false, false)); 
 } 
 }); 
 } 
 catch 
 { 
 throw; 
 }

Comments

  • Anonymous
    May 24, 2012
    This is awesome. Saved lot of efforts.