简体   繁体   中英

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

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.

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