6

I have a custom list template (STP) in my list template gallery. I can retrieve the list template using the client object model in javascript. But it's not clear how to create a new list from that template. This is what I am trying:

var site = SP.ClientContext.get_current().get_site();
var web = SP.ClientContext.get_current().get_web();
this.result = site.getCustomListTemplates(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() === 'mycustomtemplate')
        {
            var listCreationInfo = new SP.ListCreationInformation();
            listCreationInfo.set_title('testfromtemplate');
            listCreationInfo.set_templateFeatureId(item.get_featureId());
            listCreationInfo.set_templateType(item.get_listTemplateTypeKind());

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

The code executes successfully and the list is created, but it is not based on my template. It's just a generic list with a title column. How can I get the code to base the new list on my template?

  • According to this blog post, what you're doing should work. Perhaps someone else can spot a difference that I missed. – jpaugh Aug 17 '17 at 16:20
  • I am facing the same issue. please let me know if anyone resolved this issue successfully. Custom list template is not getting applied to newly created list. Note:I am using the same code mentioned by @J T. I dont have permissions to do comment, so posting as an answer. – Vishal_chorghe Aug 10 '18 at 5:44
4

You cannot create a new list based on a custom list template with only JavaScript Object Model. You would need to first use Visual Studios to create a custom List Definition. To achieve the same outcome using only JavaScript Object Model, you can do the following:

  • Load all the properties and fields of your template list, and pass those to a function which will,
  • Create a new list with same definition and add the same fields to the new list from your list template

This discussion (comment on July 5th) includes an example of doing this with SPServices, but the same could be done with the JavaScript Object Model.

2

Most probably it occurs since List.TemplateType and ListTemplateFeatureId properties for a out-of-the-box Custom List (Feature ID:00BFEA71-DE22-43B2-A848-C05709900100) are passed instead of your custom List.

How to determine List TemplateFeatureId and TemplateType properties

  • TemplateType:the list of available List template types could be found on this page

  • TemplateFeatureId: to determine FeatureId property, you could save your list as a template, then go to List Template Gallery page (/_catalogs/lt/Forms/AllItems.aspx) and find the Feature Id value of the corresponding list.

How to create a List based on custom list template via JSOM

The following example demonstrates how to create custom Tasks list:

var context = SP.ClientContext.get_current();
var web = context.get_web();

var listInfo = new SP.ListCreationInformation();
listInfo.set_title('Project Tasks');
listInfo.set_description('Project Tasks');
listInfo.set_templateType(SP.ListTemplateType.tasksWithTimelineAndHierarchy); 
listInfo.set_templateFeatureId('<custom tasks feature id>');
var list = web.get_lists().add(listInfo);

context.executeQueryAsync(
     function () {
        console.log(String.format('List {0} has been created successfully',list.get_title()));
     },
     function (sender, args) {
        console.log(args.get_message());
     }
);
  • The Feature ID in the List Template Gallery for my custom list appears to be the Feature ID of the Generic List even though I have columns added in my list template (and creating the list using this technique creates a list without the extra columns). – jvvw Apr 30 '15 at 10:33
  • Your comment is very good, but doesn't get to the point, that is a myCustomListTemplateType from /_catalogs/lt/ – egidiocs Mar 11 '16 at 4:23
  • I agree, the idea is good by Vadim but it does not work, I just tested it – user31823 Jun 13 '16 at 23:30
  • As jvvw said, feature ID in list template gallery displays 00BFEA71-DE22-43B2-A848-C05709900100. I read many posts on the web, are you sure it is possible using only javascript to create a list based on a template, created using "export list as template" functionality? – D.Barafort Feb 15 '17 at 7:29
2

I know this question is old but no one addressed the issue properly.

To use javascript and a list templates it's actually quite easy.

  1. create a list

  2. create a template based on list

  3. goto site content - click on cog (top right) -> click on site content

  4. click on "add an app"

  5. click on the template you want to use. It will display as a popup. When the popup has loaded right click and view properties. Copy the URL.

  6. open a new tab and paste the URL to test that it works.

  7. use some javascript to copy what SharePoint does to produce the same popup:

the code below is using SP.UI.ModalDialog.showModalDialog and more can be found here:

https://msdn.microsoft.com/en-us/library/office/ff410058(v=office.14).aspx

code is showing some text first "New project" and then a hyperlink with some javascript that uses the showModalDialog with the url that we took earlier within the href. The display text of the hyperlink is "new custom List".

Here's the HTML:

<p>New Project
  <a target="_blank" onclick="javascript:onCreateList()"
      href="https://mysite.sharepoint.com/sites/mysitecollection/_layouts/15/new.aspx?FeatureId=%7B00bfea71%2Dde22%2D43b2%2Da848%2Dc05709900100%7D&ListTemplate=100&IsDlg=1">
    <span>New custom list</span>
  </a>
</p>

And, here's the JavaScript:

function onCreateList () {
    this.href = unescapeProperly(escape(this.href));
    function createNewListWithUrl(url) {
        var opt = {
            url: url,
            autoSize: true,
            dialogReturnValueCallback: function(res, retVal) {
                if (res === SP.UI.DialogResult.OK) {
                    /*do something useful if you want to return data back*/
                }
            }
        };
        SP.UI.ModalDialog.showModalDialog(opt);
    }
    createNewListWithUrl(this.href);
    return false;
}

its a very easy method and quick using mainly html and some javascript. As you can see the most important part is the url as that is pointing to the correct feature id which refers to the custom template i created based off of a custom list and list template that was used which was a custom list.

  • Hmmm THANK YOU!) unfortunately Vadim's solution doesn't work in my case (I have custom lib template). If I unpack *.stp - templateID is "1" there, think it is a problem. But Ali's answer is great, thanks for sharing! – Gennady G Dec 1 '17 at 21:22
0

Set the Template property of ListCreationInformation object: lci.ListTemplate = template;

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.