Programatically create Site from Site Template Sharepoint 2010
07/25/2010 • Admin
Category: SharePoint 2010
Here is a code snippet to create a site from an existing site template in sharepoint 2010.
Please Note that siteUrl below contains the URL for the site collection, for example, Site_Name or sites/Site_Name. It may either be server-relative or absolute for typical sites.
protected void CreateTeamSite(string siteUrl, string newsiteName, SPFieldUserValue siteOwner)
{
try
{
using (SPSite site = new SPSite(“http://SPSite”))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
// Set the user for your new site
string ownerLogin = siteOwner.User.LoginName; //user’s login name
string ownerEmail = siteOwner.User.Email;
string ownerName = siteOwner.User.Name;
//Create new web site
SPWeb newWeb = siteCollection.Add(“/” + siteUrl, siteName, “site description”, 1033,{Name of the Site Template}, ownerLogin,ownerName,ownerEmail );
newWeb.Update();
}
}
}
To get Name of the existing site template {Name of the Site Template} follow the below steps
1. Save a Site as a Template.
2. Go to Site settings –> sites & workspaces –> create.
3. IE Tools > Developer Tools > Find > Select Element By Click > View > Source > DOM (Element) > Highlight and copy the section
<OPTION value=”{D120C236-16AC-49D0-2278-F2283F0FD521}#sitename”>test</OPTION> . The one in bold will be your site template name.




Recent Forum Posts
Linq to Sharepointby RS on May 18, 2012
comments on the codeby spbsmile on May 18, 2012
connection(add) EditControlBlock and my programby spbsmile on May 18, 2012
Custom master pages used in multiple SharePoint 2010 sitesby ubersteve on May 18, 2012
upload file and click on button but cant upload file into list by using below codeby santhoshreddy on May 18, 2012