3

I would like to create a list based on a template (.stp uploaded to the list template gallery), it almost works with this code:

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>";

    ListTemplate listTemplate = site.ListTemplates.First(listTemp => listTemp.Name == "<Your Template Name>");
    listCreationInfo.TemplateFeatureId = listTemplate.FeatureId;
    listCreationInfo.TemplateType = listTemplate.ListTemplateTypeKind;

    list = site.Lists.Add(listCreationInfo);
    context.ExecuteQuery();

It display this error: Error text

This is were i found the code: https://stackoverflow.com/questions/9107570/how-do-i-create-a-new-list-from-a-list-template-client-object-model

Can anybody help me? thx.

  • I have the exact same issue. Where I am trying to create a list using a custom list template through CSOM. In my case it is a custom Wiki Page library. I am assigning the featureID and ListTemplateTypeKind of the template to the ListCreationInformation. The code just works fine without any error. However new library gets created with default Wiki Page library features and not that of my custom template. I am using Office 365. Did you get a break through and if yes I would really appreciate if you could share the solution. Thanks -Pankaj – user32485 Aug 14 '14 at 15:06
3

In your example, while retrieving List Template at the line:

var listTemplate = site.ListTemplates.First(lt => lt.Name == "<Your Template Name>");

the error Microsoft.SharePoint.Client.CollectionNotInitializedException will occur since ListTemplates collection have to be requested explicitly, for example:

context.Load(site,s => s.ListTemplates );

Below is provided the fixed example:

using (var context = new ClientContext(url))
{
     //context.Credentials = credentials;
     var site = context.Web;
     context.Load(site,s => s.ListTemplates );
     context.ExecuteQuery();

     var listCreationInfo = new ListCreationInformation
     {
           Title = "<Your Title>",
           Description = "<Your Description>"
     };

     var listTemplate = site.ListTemplates.First(lt => lt.Name == "<Your Template Name>");
     listCreationInfo.TemplateFeatureId = listTemplate.FeatureId;
     listCreationInfo.TemplateType = listTemplate.ListTemplateTypeKind;

     site.Lists.Add(listCreationInfo);
     context.ExecuteQuery();
}
  • Almost there, now it breaks at: ListTemplate listTemplate = site.ListTemplates.First(lt => lt.Name == "Dokumentbibliotek3.0"); Display error: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll Additional information: Sequence contains no elements Any thoughts? – Jacob Filtenborg Jul 21 '14 at 17:35
1

First(); is used to get first element from LINQ C# query

Have you tried adding namespace using System.Linq; ?

  • Thank you, got me further. Now it display this error: An unhandled exception of type 'Microsoft.SharePoint.Client.CollectionNotInitializedException' occurred in Microsoft.SharePoint.Client.Runtime.dll Additional information: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. – Jacob Filtenborg Jun 23 '14 at 20:19
  • Which line of code is breaking? It seems you're not using Load() before calling ExecuteQuery(); – Falak Mahmood Jun 23 '14 at 22:07
  • It breaks at: ListTemplate listTemplate = site.ListTemplates.First(listTemp => listTemp.Name == "KlasseSiteMenu3.0.1"); How should i include Load() in my code? – Jacob Filtenborg Jul 21 '14 at 17:16
1

this is an old question but adding the reason why this is not working for benefit of others

the reason this breaks on the below line is because this site.ListTemplates does not contain the custom list template ListTemplate listTemplate = site.ListTemplates.First(lt => lt.Name == "Dokumentbibliotek3.0");

to get the custom list template use the below line var listTemplates = context.Site.GetCustomListTemplates(context.Web); context.load(listTemplates); context.ExecuteQuery(); ListTemplate listTemplate = listTemplates.First(lt => lt.Name == "Dokumentbibliotek3.0");

rest of the code remains the same

0

As much as I have searched, I found that this is not supported in CSOM currently!

Ref: https://social.msdn.microsoft.com/Forums/office/en-US/66bdfb38-72d4-4ac4-9651-dbb598eec90d/creating-lists-from-custom-template-in-powershell-csom?forum=sharepointdevelopment

May be we have one solution using CSOM: first to create a basic custom list, then add field by field every other columns we need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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