2012-02-02 19 views
5

Liste özel liste şablonunu kullanarak oluşturuyorum. Liste oluşturuluyor, ancak özel liste şablonu listeme uygulanmadı.Liste şablonundan (İstemci Nesne Modeli) yeni bir liste nasıl oluşturabilirim?

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

Özel listeyi uygulamak için kodum nasıl değiştirilir?

+0

Öncelikle yeni liste için liste şemasını içeren özelliğin özellik tanımlayıcısı belirten bir değeri ayarlar sen' şablon nesnesini null olarak yeniden kontrol etmeyiniz, bu yüzden, aslında peşinde olduğunuz şablonu gerçekten almamış olabilirsiniz. İkincisi, bana bir liste şablonu adı gibi görünmüyor. – GavinB

cevap

6

Aşağıda verilen bu kodu deneyin. Senin için çalışmalı. Herhangi bir sorunla karşılaşırsanız haber verin. Microsoft gereğince

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 = Alır veya

+1

Bu neredeyse benim için çalıştı. Eklemem gereken tek şey listCreationInfo.TemplateType = listTemplate.ListTemplateTypeKind; –

+0

Bu benim için bir "Access App" listesinde ve diğer birkaç işe yaramadı. Ayrıca ClientContext IDisposable uygular, bu nedenle bağlam nesnesinde "using" ifadesini kullandığınızdan emin olun. –

İlgili konular