Sep 19, 2009

Code to create site using a template (Document workspace)

This is sample code for creating the site programmatically using site template stored in the site template gallery.


If we are creating site through the console application ,then replace the SPContext with the site or web object


Code snippet


Guid siteID = SPContext.Current.Site.ID;


Guid webID = SPContext.Current.Web.ID;


SPSecurity.RunWithElevatedPrivileges(delegate()


{


using (SPSite site = new SPSite(siteID))


{


//Get the template from the site template gallery


SPWebTemplate oWebTemplate = site.GetCustomWebTemplates(Convert.ToUInt32(1033))["XXXX"];


using (SPWeb web = site.OpenWeb(webID))


{


//Create site without copying the users from the top site


web.AllowUnsafeUpdates = true;


string webUrl = "Site Name";


subSite = web.Webs.Add(webUrl, "SiteName", Site Description", 1033, oWebTemplate, false, false);


}


}


});


Note: If we want to create a site based on the OOB available templates ,just modify the code accordingly


subSite = web.Webs.Add(webUrl, "SiteName", Site Description", 1033, "TemplateName", false, false);


Lets look at the parameters passed in it


strWebUrl:


A string that contains the new Web site URL relative to the root Web site in the site collection. For example, to create a Web site at http://MyServer/sites/MySiteCollection/MyNewWebsite, specify MyNewWebsite


strTitle


A string that contains the title.


strDescription


A string that contains the description.


nLCID


An unsigned 32-bit integer that specifies the locale ID. You can find a complete list of local ID at the following URL:


http://www.microsoft.com/globaldev/reference/lcid-all.mspx


We have used “1033” for English – United States.


strWebTemplate


A string that contains the name of the site definition configuration or site template. The following table shows the values for the default site definition configurations that are included in an installation of Windows SharePoint Services


Value


Site Definition


STS#0 Team Site


STS#1 Blank Site


STS#2 Document Workspace


MPS#0 Basic Meeting Workspace


MPS#1 Blank Meeting Workspace


MPS#2 Decision Meeting Workspace


MPS#3 Social Meeting Workspace


MPS#4 Multipage Meeting Workspace


WIKI#0 Wiki


BLOG#0 Blog


useUniquePermissions


true to create a subsite that does not inherit permissions from another site; otherwise, false.


bConvertIfThere


true to convert an existing folder of the same name to a SharePoint site. false to throw an exception that indicates that a URL path with the specified site name already exists.


Enjoy coding !!!

No comments:

Post a Comment