Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

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"

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top