Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a new list from a list template (Client Object Model)

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?

like image 526
Anandhan Avatar asked Feb 02 '12 05:02

Anandhan


1 Answers

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

like image 188
Abhishek Patel Avatar answered Oct 13 '22 10:10

Abhishek Patel