【问题标题】:How do I create a new list from a list template (Client Object Model)如何从列表模板创建新列表(客户端对象模型)
【发布时间】:2017-04-05 03:10:59
【问题描述】:

我正在根据自定义列表模板创建列表。正在创建列表,但自定义列表模板不适用于我的列表。

ListTemplate template = null;
ListTemplateCollection ltc = context.Site.GetCustomListTemplates(context.Web);
context.Load(ltc);
context.ExecuteQuery();  

foreach (ListTemplate t in ltc)
{
    if (t.InternalName == "STPDiv.stp")
    {
        template = t;
        break;
     }
}

ListCreationInformation info = new ListCreationInformation();
info.Title = "TestCreation";
info.TemplateType = template.ListTemplateTypeKind;
info.TemplateFeatureId = template.FeatureId;           
info.QuickLaunchOption = QuickLaunchOptions.DefaultValue;
site.Lists.Add(info);
context.ExecuteQuery();

如何修改我的代码以应用自定义列表?

【问题讨论】:

  • 首先你不是空检查模板对象,所以你很可能实际上没有得到你想要的模板。其次,这在我看来不像是列表模板名称。

标签: sharepoint sharepoint-2010


【解决方案1】:

试试下面给出的代码。它应该适合你。如果您遇到任何问题,请告诉我。

ClientContext context = new ClientContext("<Your Site URL>");
Web site = context.Web;            
context.Load(site);
context.ExecuteQuery();

//Create a List.
ListCreationInformation listCreationInfo;
List list;

listCreationInfo = new ListCreationInformation();
listCreationInfo.Title = "<Your Title>";
listCreationInfo.Description = "<Your Description>";

var listTemplate = 
            site.ListTemplates.First(listTemp => listTemp.Name == "<Your Template Name>");
listCreationInfo.TemplateFeatureId = listTemplate.FeatureId;

list = site.Lists.Add(listCreationInfo);
context.ExecuteQuery();

根据微软: ListCreationInformation members

TemplateFeatureId = 获取或设置一个值,该值指定包含新列表的列表架构的特征的特征标识符

【讨论】:

  • 这个几乎对我有用。我只需要添加 listCreationInfo.TemplateType = listTemplate.ListTemplateTypeKind;
  • 这对我在“访问应用程序”列表和其他一些列表中不起作用。 ClientContext 还实现了 IDisposable,因此请务必在上下文对象上使用“使用”语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
  • 2011-09-25
  • 2014-08-10
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多