简体   繁体   中英

How do I create a new list from a list template (Client Object Model)

I'm creating the list based on custom list template. List is creating, but the custom list template is not applied for my list.

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

How can my code be modified to get the custom list applied?

Try this code given below. It should work for you. Let me know if you encounter any problem.

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

As per Microsoft : ListCreationInformation members

TemplateFeatureId = Gets or sets a value that specifies the feature identifier of the feature that contains the list schema for the new list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM