Thursday, 5 January 2012

SharePoint 2010 - Create Site From Template

Creating a SharePoint site from a site template provides a simple and quick way of provisioning new sites with common lists and library's.

Click here to see how to save a site as a site template.

Once a site has been saved as a site template you can use it as a base to create new sites.  This can either be done through the standard SharePoint interface or through code.

C# – Create Site From Template

private void CreateSiteFromTemplate(string strWebUrl,string strTitle,string strDescription)
        {
            using (var curSite = new SPSite("http://SharePoint"))
            {
                using (var curWeb = curSite.OpenWeb())
                {
                    uint nLCID = 1033;

                    SPWebTemplate WebTemplate = curWeb.Site.GetWebTemplates(1033)["Guid#TemplateName"];

                    curWeb.Webs.Add(strWebUrl, strTitle, strDescription, nLCID, WebTemplate, false, false);                     
                }
            }
        }

 

Key Line

The code block below is used to specify which template is to be used to create the new site.  When using a custom template the format of the template name is a Guid followed by a hash then the name of the template name.

Click here to see how to find out the internal name for a custom site template.

SPWebTemplate WebTemplate = curWeb.Site.GetWebTemplates(1033)["Guid#TemplateName"];

No comments:

Post a Comment