6

Creating lists in SharePoint using Managed Client Object Model is rather easy task. Here's how to create a list with SharePoint's custom list template:

ListCreationInformation lci;
List list;

lci = new ListCreationInformation();
lci.Title = title;
lci.Description = description;
lci.TemplateType = (int)ListTemplateType.GenericList;
list = clientContext.Web.Lists.Add(lci);
clientContext.ExecuteQuery();

But what happens if I don't what to use any of the default ListTemplateType templates? What if I have created my own list template and I want to use it in the code to create lists based on it? Please help, thanks.

1
  • I can't comment due to my low rep, but I'd like to know if you ever figured this out. I have looked long and hard for a solution and am yet to find one. Commented Dec 31, 2012 at 0:06

3 Answers 3

0

I know the TemplateFeatureId property looks like it should do what we want, ie. create a list from a given template, but it doesn't.

On this post and this post, on MSDN forums, the accepted answer is that you can't create a list from a list template at all using the client side object model - and that's my experience, too.

1
  • Can this be achieved using REST? If not does it mean that using client side scripting we cannot create lists using list templates?
    – Jinxed
    Commented Jul 28, 2015 at 15:21
4

I cannot test this because I dont have a dev environment at the moment, but the following should work:

ListCreationInformation lci;
List list;

lci = new ListCreationInformation();
lci.Title = title;
lci.Description = description;


ListTemplate lt = ClientContext.Current.Web.ListTemplates.First(z => z.Name == "MyTemplateName");
lci.TemplateFeatureId = lt.FeatureId;

list = clientContext.Web.Lists.Add(lci);
clientContext.ExecuteQuery();

As per http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.listcreationinformation_members.aspx

TemplateFeatureId = Gets or sets a value that specifies the feature identifier of the feature that contains the list schema for the new list.

1
  • Anyone reading this: don't get your hopes up. It only works for built-in list templates, not custom ones. Commented Feb 21, 2014 at 15:44
1

If you want to get custom templates you have to use context.Site.GetCustomListTemplates() instead of context.Web.ListTemplates

public static ListTemplate GetCustomSiteTemplate(ClientContext ctx, string templateName)
{
    var site = ctx.Site;
    ctx.Load(site);
    ctx.ExecuteQuery();
    ListTemplateCollection ltc = site.GetCustomListTemplates(ctx.Web);
    ctx.Load(ltc);
    ctx.ExecuteQuery();
    var listTemplate = ltc.FirstOrDefault(lt => lt.Name == templateName);
    return listTemplate;
}

On SharePoint 2013, I found my template and used it in ListCreationInformation, the list was created but it didn't have the settings of the template (views etc even though it contained them).

According to this discussion on SharePoint 2010:

"You cannot create list using the custom template using Client OM. Instead, you can create a custom list definition, and then create list based on this definition. More information about Create List using Client Object Model"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.