Search code examples
c#sharepoint-2013sharepoint-listsharepointdocumentlibrarysharepoint-listtemplate

sharepoint 2013 create document library using List Template C#


It is possible to used a list template to create new document library, I am trying following code but its is not working.it just create library without using template.

ListTemplateCollection listTemplates1 = context.Site.GetCustomListTemplates(context.Web);
ListTemplate li1;//
context.Load(listTemplates1);
context.ExecuteQuery();
context.Load(site.ListTemplates);
context.ExecuteQuery();

var listTemplate = listTemplates1.First(lt => lt.Name == "<Test>");

ListCreationInformation li = new ListCreationInformation();
li.Title = "XYZZ2";
li.Description = "Created through Code";
li.TemplateFeatureId = listTemplate.FeatureId;
li.TemplateType = listTemplate.ListTemplateTypeKind;
List newList = context.Web.Lists.Add(li);
context.Load(newList);
context.ExecuteQuery();

Solution

  • Can you directly try to fetch the template instead of getting the entire collection like the following:

    ListTemplate listTemplate = context.web.ListTemplates.GetByName("templateName"); 
    context.Load(listTemplate);
    context.ExecuteQuery();
    

    Then create your list,

    ListCreationInformation li = new ListCreationInformation();
    li.Title = "XYZZ2";
    li.Description = "Created through Code";
    li.TemplateFeatureId = listTemplate.FeatureId;
    li.TemplateType = listTemplate.ListTemplateTypeKind;
    List newList = context.Web.Lists.Add(li);
    context.Load(newList);
    context.ExecuteQuery();
    

    This may be because the listTemplate, in your case, has not been initialized properly which is why the List was getting created with the default template.