How to create a new list from a list template (Client object model)

I am creating a list based on a custom list template. The list is created, but the custom list template is not applied to 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 changed to apply a custom list?

+5
source share
1 answer

Try this code below. This should work for you. Let me know if you have any problems.

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

According to Microsoft: ListCreationInformation Elements

TemplateFeatureId = Gets or sets a value indicating the identifier of the function containing the list scheme for the new list

+6
source

All Articles