Classes and Object used…
· SPWebApplicationBuilder: Creates an SPWebApplication object, providing default settings for all the required values and allowing the caller to change only those properties that need values different from the default
· SPFarm: Represents a Windows SharePoint Services farm. To access the current server farm object, you can use members on SPFarm.Local.
· SPWebApplication: Represents an Internet Information Services (IIS) load-balanced Web application that is installed on the server farm.
· SPSiteCollection : Represents a collection of SPSite objects, or site collections.
· SPSite : Represents a collection of sites on a virtual server, including a top-level site and all its subsites. Each SPSite object, or site collection, is represented within an SPSiteCollection object that consists of the collection of all site collections.
· System.Security.SecureString: Represents text that should be kept confidential. The text is encrypted for privacy when being used, and deleted from computer memory when no longer needed. A SecureString object is similar to a String object in that it has a text value. However, the value of a SecureString object is automatically encrypted, can be modified until your application marks it as read-only, and can be deleted from computer memory by either your application or the .NET Framework garbage collector
Let’s start...
Creating web Application at Port no 2007 ( Assuming user spuser with password Password is present in system administrator group)
SPWebApplicationBuilder webAppBuilder = new SPWebApplicationBuilder(SPFarm.Local);
SPWebApplication newApplication;
int myPort = 2007;
webAppBuilder.Port = myPort;
webAppBuilder.ApplicationPoolId = "site-appol"; // application pool
webAppBuilder.ApplicationPoolUsername = webAppBuilder.DefaultZoneUri.Host + \\spuser;
System.Security.SecureString password = new System.Security.SecureString();
string strName = "Password";
char[] pass = strName.ToCharArray();
foreach (char c in pass)
password.AppendChar(c);
webAppBuilder.ApplicationPoolPassword = password;
webAppBuilder.CreateNewDatabase = true; // Create new database
webAppBuilder.DatabaseName = "wss_site2007_content"; // database name
webAppBuilder.DatabaseServer = webAppBuilder.DefaultZoneUri.Host; //Host name/computer name
webAppBuilder.UseNTLMExclusively = true; // Use NTLM authentication
newApplication = webAppBuilder.Create(); // Create new web application
newApplication.Provision(); //Provision it into web farm
Creating site Collection at root level with Blank site template
SPSite mySiteCollection = newApplication.Sites.Add("/", //Root
"Cgosite", // site title
"Conchango.com", //description
1033, //language
"STS#1", //Blank site template
webAppBuilder.ApplicationPoolUsername, // Site owner
"Name", // Name
"name@name.com"); //Email
Creating web
mySiteCollection.AllWebs.Add("/webname");
Activating publishing feature at site collection
mySiteCollection.Features.Add(SPFarm.Local.FeatureDefinitions["PublishingSite"].Id);
SPFarm.Local.FeatureDefinitions["PublishingSite"].Provision()
Close Site Collection
mySiteCollection.Close();