2010-07-13 110 views
4

我正在從silverlight webpart創建新的sharepoint網站。我正在使用ClientContext模型,它對團隊網站模板(STS#0)非常有用。我需要從我創建的CUSTOM網站模板創建一個NEW網站,但是我不知道如何引用此模板來指定它是名稱的Web模板,並且只能引用其中一個標準模板。Sharepoint 2010 - 使用自定義網站模板從代碼創建網站

這裏是我的代碼:

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); 
    } 

回答

6

遍歷所有可用的模板,你會發現,自定義模板名稱的全局唯一標識符在它的前面:{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); 
    } 
} 
相關問題