Automated Deployment for Complete Website
As I’ve discussed different deployment options available in MOSS 2007. In this post, I will discuss how we may write down script for deploying complete website. First of all, create a project in Visual Studio 2005 and add references for Microsoft.Office.Sharepoint & Microsoft.Office.Sharepoint.Services. We will step by step look at scripts for achieving different targets. We will need to write scripts for the following steps:
- Create Web Application (along with site collection)
- Restore our website to newly created site
- Add features as required
Create Web Application
In order to create a web application, you need to write down the following code. The following code will create a new web application with a site collection within it.
// web site port
int port = Port_No;
// web site root directory
string rootDir = PATH; // it should be the path to the web app within VirtualDirectories folder
// web application name
string webAppName = Name_Of_The_Application; // this could be any string
// app pool username
string appPoolUser = User_Name; // name of the user who has rights over central administration
// build the password as a secure string
SecureString appPoolPwd = new SecureString();
foreach (char i in txtPassword.Text)
{
appPoolPwd.AppendChar(i);
}
appPoolPwd.MakeReadOnly();
Uri defaultZone = new Uri(Default_Zone);
Uri intranetZone = new Uri(Intranet_Zone);
// the database server that will host the content database(s) for this web application
string dbServer = DB_Server;
// the name of the first content database for this web application
string contentDb = Content_DB_Name; // name of the content db that will be created
// for the top-level site
string url = “/”;
// the site title
string siteTitle = Site_Title;
// the site description
string siteDesc = Site_Desc;
// this is the MS language Locale ID. see http://www.microsoft.com/globaldev/reference/lcid-all.mspx
uint LCID = 1033;
// this is the site template for the Publishing Site – Collaboration Portal merged site definition
string template = “SPSPORTAL”; // you may change it to any other template you want
// first site owner login name
string ownerLogin = Site_Owner; // this should be a user name from active directory
// site user name
string ownerName = User_Name; // this could be any descriptive name you want to give
// site owner email
string ownerEmail = Email_Address;
// get an instance of the farm from the administrative service
SPFarm farm = SPWebService.AdministrationService.Farm;
// use the SPWebApplicationBuilder to create web application and pass the local farm instance to the builder class
SPWebApplicationBuilder webAppBld = new SPWebApplicationBuilder(farm);
// assign a GUID to the IIS web application.
try
{
webAppBld.Id = System.Guid.NewGuid();
}
catch (Exception ex)
{
}
// the web application’s port
webAppBld.Port = Port;
// create a root directory for the IIS application using the SYSTEM.IO namespace
DirectoryInfo rootDirInfo = new DirectoryInfo(rootDir);
// create the root directory and assign it to the web app.
webAppBld.RootDirectory = rootDirInfo;
// set the ID of the web application pool this is not a GUID as the WSS SDK suggests
webAppBld.ApplicationPoolId = webAppName + ” Pool – ” + port.ToString();
// uses the IdentityType enumeration. This can be one of four values but for SharePoint web farms is usually a specific user account in the domain.
webAppBld.IdentityType = IdentityType.SpecificUser;
// be sure that this user exists and that the password matches the value stored in the SecureString
// appPoolPwd value.
webAppBld.ApplicationPoolUsername = appPoolUser;
// assign the secure string value as the app pool password
webAppBld.ApplicationPoolPassword = appPoolPwd;
// the default is false so that Kerberos auth. is supported. If you’re not using Kerberos, set this to true
webAppBld.UseNTLMExclusively = true;
// default is false
webAppBld.AllowAnonymousAccess = false;
// the default is false so that SSL is not used in IIS. set this to true if the IIS web site hosting this web application should use SSL
webAppBld.UseSecureSocketsLayer = false;
// Set the default zone that appears in Alternate Access Mappings
// will probably want to change this to the URL value that most users select for browsing this web application
webAppBld.DefaultZoneUri = defaultZone;
// this will create a new content data (default is true)
webAppBld.CreateNewDatabase = true;
// set the database server that will host the database
webAppBld.DatabaseServer = dbServer;
// create a new content database for the web application
// be sure this is unique. Consider appending a GUID onto
// the end of the database name as Microsoft does when you
// use Central Admin. to create a web application
webAppBld.DatabaseName = contentDb;
// set the database username to null or an empty string
// to use Windows integrated authentication. Set both
// the username and the DatabasePassword password property
// if you want to use SQL auth.
webAppBld.DatabaseUsername = String.Empty;
//// get the server hosting the search service
SPSearchServiceInstance ssi = null;
foreach (SPServiceInstance sp in SPServer.Local.ServiceInstances)
{
if (sp.GetType().IsAssignableFrom(typeof(SPSearchServiceInstance)))
{
ssi = (SPSearchServiceInstance)sp;
break;
}
}
try
{
webAppBld.SearchServiceInstance = ssi;
}
catch (InvalidCastException ex)
{
}
// assign a comment to the web application to create
// if not set, this will default to SharePoint – <port>
webAppBld.ServerComment = webAppName + ” – ” + port.ToString();
// mention that this can take a long time
Console.WriteLine(“Web app. creation has started.”);
Console.WriteLine(“This operation an take as long as 15 minutes in our tests.”);
// create the new web application
SPWebApplication webApp = null;
try
{
webApp = webAppBld.Create();
}
catch (Exception ex)
{
}
Console.WriteLine(“Web Application created.”);
// specify time zone. You can either enumerate the GlobalTimeZones
// collection to find the right timezone or look it up on the
// Central Administration – Application Management – Web Application
// General Settings page.
webApp.DefaultTimeZone = SPRegionalSettings.GlobalTimeZones[14].ID;
// size in megabytes
webApp.MaximumFileSize = 75;
// enable alerts
webApp.AlertsEnabled = true;
webApp.Name = webAppName + ” – ” + port.ToString();
Console.WriteLine(“Updating and provisioning the web application.”);
try
{
// update the web app with the settings specified
webApp.Update();
// provisions the web application in IIS and creates the content
// database if one was specified
webApp.Provision();
}
catch (Exception ex)
{
}
Console.WriteLine(“Updating and provisioning the web application complete.”);
// perform an IIS reset operation iisreset /noforce if you’re in a web farm
Console.WriteLine(“Creating the top-level site collection.”);
Console.WriteLine(“This can also be a long running operation.”);
try
{
// create a site collection with an out of the box site definition
SPSite SiteCollection = webApp.Sites.Add(url, siteTitle, siteDesc, LCID, template, ownerLogin, ownerName, ownerEmail);
// close the site collection
SiteCollection.Close();
}
catch (Exception ex)
{
}
Console.WriteLine(“Top level site collection creation complete.”);
Restore Website
In order to restore website, write the following lines of code:
SPWebApplication app = SPWebApplication.Lookup(new Uri(Default_Zone));
SPSiteCollection clt = app.Sites;
clt.Restore(Default_Zone, @”.\YourBackup.bak”, true);
The above three (3) statements would restore your website to the newly created web appliation.

Recent Discussions