Sharepoint 2010 - 使用自定义站点模板从代码创建网站

4
我正在使用ClientContext模型从Silverlight WebPart创建一个新的SharePoint网站。对于团队网站模板(STS#0),它运行得非常好。我需要从我创建的自定义站点模板中创建一个新站点,但我不知道如何引用这个模板。只能通过名称指定Web模板,并且只能引用标准模板之一。
以下是我的代码:
  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);
  }
2个回答

6

循环浏览所有可用的模板,您会发现自定义模板名称前面有guid:{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);
    }
}

-2

链接是服务器对象模型;海报正在使用客户端对象模型。 - Grank

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接