4

I would like to create a list from custom template already available in the site. I tried following code -

     this.result = SP.ClientContext.get_current().get_site().getCustomListTemplates(SP.ClientContext.get_current().get_web());
SP.ClientContext.get_current().load(this.result);

function retrieveTemplates_fail() { }           
function retrieveTemplates_success()
{
    var en = this.result.getEnumerator();
    while (en.moveNext())
    {
        var item = en.get_current();
        if (item.get_name() === 'Template - Review Access')
        {           
            var listCreationInfo = new SP.ListCreationInformation();
            listCreationInfo.set_title('testfromtemplate1');
            //alert(item.get_featureId());
            //alert(item.get_listTemplateTypeKind());
            listCreationInfo.set_templateFeatureId('{00bfea71-de22-43b2-a848-c05709900100}');
            listCreationInfo.set_templateType('100');

            SP.ClientContext.get_current().get_web().get_lists().add(listCreationInfo);

            function createList_fail() { alert('fail') };
            function createList_success() { alert('success') };

            SP.ClientContext.get_current().executeQueryAsync(
                Function.createDelegate(this, createList_success), 
                Function.createDelegate(this, createList_fail)
            );
        }
    }
}

SP.ClientContext.get_current().executeQueryAsync(
    Function.createDelegate(this, retrieveTemplates_success),
    Function.createDelegate(this, retrieveTemplates_fail)
);

On executing the code I can only create list - Custom list with out the columns available in list template.

Can someone guide whats wrong in the code or provide any new code. Is json code available ?

1

You provided default template for custom list, that's why as a result you are having list without columns.

What you need to is to specify your custom template's type and feature id (you already has it commented):

listCreationInfo.set_templateFeatureId(item.get_featureId());
listCreationInfo.set_templateType(item.get_listTemplateTypeKind());
  • But if you look at site collection settings - list template - you all all list templates with same GUID. So how will to identify the one you need ? – Monica Jagani Feb 3 '17 at 12:02
  • Like in your code - you are filtering template by name - if (item.get_name() === 'Template - Review Access') – Sergei Sergeev Feb 3 '17 at 12:04
  • it just get the generic GUID like for list or library or any other type. If you observe in list template page, all the GUIDs are same for particular type of base template. Even i am surprised !!! – Monica Jagani Feb 3 '17 at 12:20

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.