Using Object model create Site with custom site template 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
. The one in bold will be your site template name.

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com