Sharepoint 2010 - Create Site from Code using Custom Site Template
I am creating a NEW sharepoint site from a silverlight webpart. I am using the ClientContext Model and it is working great for a team site template (STS#0). I need to create a NEW site from a CUSTOM site template that I have created, but I do not know how to reference this template being to specify a web template it is by name and only able to reference one of the standard templates.
Here is my code:
string siteUrl = App.RootSite;
string siteDescription = project.projectName; // "A new project site.";
int projectLanguage = 1033;
string projectTitle = project.projectName; // "Project Web Site";
string projectUrl = project.projectURL; //"projectwebsite";
bool projectPermissions = false;
string webTemplate = "STS#0"; //TODO: reference custom site template
try
{
ClientContext clientContext = new ClientContext(siteUrl);
Web oWebsite = clientContext.Web;
WebCreationInformation webCreateInfo = new WebCreationInformation();
webCreateInfo.Description = siteDescription;
webCreateInfo.Language = projectLanguage;
webCreateInfo.Title = projectTitle;
webCreateInfo.Url = projectUrl;
webCreateInfo.UseSamePermissionsAsParentSite = projectPermissions;
webCreateInfo.WebTemplate = webTemplate;
oNewWebsite = oWebsite.Webs.Add(webCreateInfo);
clientContext.Load(
oNewWebsite,
website => website.ServerRelativeUrl,
website => website.Created,
开发者_开发技巧 website => website.Id);
clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFail);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
Loop through all of your available templates, you will find that the custom template name has the guid in front of it: {A13D0D34-EEC2-4BB5-A563-A926F7F9681A}#ProjectSiteTemplate.
ClientContext clientContext = new ClientContext(siteUrl);
Web oWebsite = clientContext.Web;
WebTemplateCollection templates = oWebsite.GetAvailableWebTemplates(1033, true);
clientContext.Load(templates);
clientContext.ExecuteQueryAsync(onTemplateSucceeded, null);
private void onTemplateSucceeded(object sender, ClientRequestSucceededEventArgs args)
{
UpdateUIMethod updateUI = ShowTemplates;
this.Dispatcher.BeginInvoke(updateUI);
}
private void ShowTemplates()
{
foreach (WebTemplate template in templates)
{
MessageBox.Show(template.Id + " : "
+ template.Name + " : "
+ template.Title);
}
}
example http://www.learningsharepoint.com/2010/07/25/programatically-create-site-from-site-template-sharepoint-2010/
精彩评论