2012-02-02 103 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

相關問題