Reputation: 69
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?
Upvotes: 6
Views: 8350
Reputation: 61
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
Upvotes: 6