Pages

Subscribe:

Wednesday, January 18, 2012

Programmatically Create a Site from Custom Site Template in SharePoint 2010


Here is the code snippet to create a Site using a Custom Site Template in SharePoint 2010.
     
using (SPSite site = new SPSite("http://sp2010:1234/"))
      {
                    using (SPWeb newWeb = site.OpenWeb())
                    {                         
                        newWeb.AllowUnsafeUpdates = true;                      
                        SPWebCollection subsites = newWeb.Webs;
                        SPWeb newSubWeb = subsites.Add("Site url", " Site name ", "Site description", 1033, {Name of the Site Template}, true, false);
                        newWeb.Update();
                    }
                }


 for eg: {Name of the Site Template} --> "{9D26F676-B943-451B-BED4-A8657A9C0FDF}#1InternalProject"


To get the custom Site Template GUID and Site Template Name you can use the below code in SharePoint 2010 even if the Template is in Solution Gallery. 
SPWebTemplateCollection templates = newWeb.GetAvailableWebTemplates(1033, true);
                        foreach (SPWebTemplate template in templates)
                        {
                           lblTemplate.Text  +=  template.Name;
                        }

1 comments:

  1. Hi Shiva,

    Will this work if you are not using Security priviledges concept.

    Regards
    Prasad

    ReplyDelete