如何从列表模板创建新列表(客户端对象模型)

时间:2012-02-02 05:25:52

标签: sharepoint sharepoint-2010

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

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

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

1 个答案:

答案 0 :(得分: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();

根据Microsoft: ListCreationInformation members

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