2012-02-02 114 views
5

我正在基于自定义列表模板创建列表。列表正在创建,但自定义列表模板不适用于我的列表。如何从列表模板(客户端对象模型)创建新列表

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

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

+0

首先的特征的特征标识符的值,你”不要无效检查模板对象,所以你可能实际上没有得到你以后的模板。其次,这看起来不像我的列表模板名称。 – GavinB 2012-03-04 02:59:08

回答

6

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

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 =获取或设置指定包含新列表列表架构

+1

这一个几乎为我工作。我需要添加的所有内容是listCreationInfo.TemplateType = listTemplate.ListTemplateTypeKind; – 2012-05-17 03:51:10

+0

这对我而言并不适用于“访问应用程序”列表和其他一些列表。 ClientContext也实现了IDisposable,所以一定要在上下文对象上使用“using”语句。 – 2014-05-12 17:13:12

相关问题