5

I am trying to create custom list using a custom list template in csom. My lst get created but it doesn't contain the schema of the list template. It looks like a generic list.

Please let me know how can I create custom list using my custom template in CSOM?

ListCreationInformation lci; 
List listInstance;

lci = new ListCreationInformation();
lci.Title = listName;
lci.Description = listDescription;

ListTemplate lt = collection.First(z => z.Name == listTemplate);
lci.TemplateFeatureId = lt.FeatureId;
lci.TemplateType = lt.ListTemplateTypeKind;                        
listInstance = web.Lists.Add(lci);
context.Load(web.Lists);
context.ExecuteQuery();
Hardik
  • 7,733
  • 2
  • 18
  • 37
user54665
  • 51
  • 1

1 Answers1

2

Please try below updated code.

using (var context = new ClientContext(url))
{
     //context.Credentials = credentials;
     var site = context.Web;
     context.Load(site,s => s.ListTemplates );
     context.ExecuteQuery();

     var listCreationInfo = new ListCreationInformation
     {
           Title = "<Your Title>",
           Description = "<Your Description>"
     };

     var listTemplate = site.ListTemplates.First(lt => lt.Name == "<Your Template Name>");
     listCreationInfo.TemplateFeatureId = listTemplate.FeatureId;
     listCreationInfo.TemplateType = listTemplate.ListTemplateTypeKind;

     site.Lists.Add(listCreationInfo);
     context.ExecuteQuery();
}

Please add namespace System.Linq; if you haven't added it to your code.

Hardik
  • 7,733
  • 2
  • 18
  • 37