0

I am trying to create a list based on a template I have saved. enter image description here Below code should get the list where the name is "ContactList" but I always get the error that a list by that name doesn't exist. (have tried with feature id also)

var listTemplate = site.ListTemplates.First(lt => lt.Name == "ContactList");

If I loop thorough all of the list templates I only get the "standard" templates back. If I press "Add app" i can see that my template list is there and I am able to add it.

clientContext.Load(site, s => s.ListTemplates);
     foreach (var s in site.ListTemplates)

How can I add a list based on a existing template by code? Or can this even be done? I have only been able to add a standard "custom list"

Whole code:

        var site = clientContext.Web;

        clientContext.Load(site, s => s.ListTemplates);
        clientContext.ExecuteQuery();

        var listCreationInfo = new ListCreationInformation
        {
            Title = "ContactTest",
            Description = "ContactTest",
        };

        var listTemplate = site.ListTemplates.First(lt => lt.Name == "ContactList");
        listCreationInfo.TemplateFeatureId = listTemplate.FeatureId;
        listCreationInfo.TemplateType = listTemplate.ListTemplateTypeKind;
        site.Lists.Add(listCreationInfo);
        clientContext.ExecuteQuery();
2
  • use site instead web ( var site = clientContext.Web;)
    – Anil Pal
    Oct 15, 2018 at 11:22
  • @pat Please don't forget to upvote and mark my answer as accepted in case it helped you! Oct 24, 2018 at 16:42

2 Answers 2

0

We cannot create a List using the custom template in Client Object Model. Try creating a custom list definition, and then create list based on this definition.

More information about Create List using Client Object Model

MSDN Reference

0

GetCustomListTemplates will help you,

private static void CreateListUsingCustomTemplate()
    {
        string passwordText = "****";
        SecureString password = new SecureString();
        foreach (char c in passwordText)
        {
            password.AppendChar(c);
        }
        using (var context = new ClientContext("https://***/sites/CollabSite"))
        {
            context.Credentials = new SharePointOnlineCredentials("****.onmicrosoft.com", password);
            // Load the custom templates from site collection 
            ListTemplateCollection templates = context.Site.GetCustomListTemplates(context.Web);
            context.Load(templates);
            context.ExecuteQuery();
            // Initialize list or library creation info 
            var listCreationInfo = new ListCreationInformation
            {
                Title = "Test Custom Library",
                Description = "Test Custom Library"
            };

            ListTemplate listTemplate = templates.First(listTemp => listTemp.Name == "TestListTemp");

            listCreationInfo.ListTemplate = listTemplate;
            listCreationInfo.TemplateFeatureId = listTemplate.FeatureId;
            listCreationInfo.TemplateType = listTemplate.ListTemplateTypeKind;
            // Add Document Library to site 
            context.Web.Lists.Add(listCreationInfo);
            context.ExecuteQuery();
        }
    }
2
  • Having some problems with listCreationInfo.ListTemplate = listTemplate; I cant find ListTemplate
    – pat
    Oct 12, 2018 at 11:50
  • which version of SharePoint are you using?
    – Anil Pal
    Oct 12, 2018 at 12:28

Your Answer

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

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