2

I'm having a difficult time creating custom lists using the SP JS API. Here's what I'm attempting to do:

var lciTasksList = new SP.ListCreationInformation();
lciTasksList.set_title('ProjectTasks');
lciTasksList.set_description('Project Tasks');
lciTasksList.set_templateFeatureId('00BFEA71-DE22-43B2-A848-C05709900100');

var tasksList = newWeb.get_lists().add(lciTasksList);

context.executeQueryAsync(
     Function.createDelegate(this, function () {
          alert('lists created');
     }),
     Function.createDelegate(this, function (sender, args) {
           alert('lists creation failed');
     })
);

The templateFeatureId is a custom list template that is based on the Task list type, with 1 additional custom Choice field. The execution method always ends in failure with

Cannot complete this action. Please try again.

Has anyone had success creating a list dynamically using a custom list template? My ultimate goal here is to use the JS API to create a new subweb (which works fine) and add several lists with custom columns. Is there another way to accomplish the same task?

Any insights would be much appreciated.

* UPDATE *

So I've gone with adding fields manually using the following:

taskList.get_fields().addFieldAsXml('<Field ID="{d2861db8-e36f-4c5e-8168-f7da8b010443}" Name="pdProjectStatus" DisplayName="Project Status" Type="Choice" Required="TRUE" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInViewForms="TRUE" ShowInNewForm="TRUE" Indexed="TRUE"><CHOICES><CHOICE>Active</CHOICE><CHOICE>Archived</CHOICE><CHOICE>Proposed</CHOICE></CHOICES></Field>'
                , true
                , SP.AddFieldOptions.AddToDefaultContentType
            );

Still would be interesting to know if the template approach is available, either by a custom list template or by adding pre-existing custom site columns. Adding the fields in this way seems awfully inefficient, especially considering the Choice field type.

3

This error occurs probably due to a missing List.TemplateType property. When creating List, List.TemplateType property have to be specified.

Note: in your example Feature ID with value 00BFEA71-DE22-43B2-A848-C05709900100 is used. That feature corresponds to Custom List, not a Task 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('F9CE21F8-F437-4F7E-8BC6-946378C850F0');
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());
     }
);

How to determine List TemplateFeatureId and TemplateType properties

The list of available List template types could be found on this page

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 Feature Id value.

  • Thank you, this makes sense. So the set_templateType takes a base type, which I assume has to be the same base type as the list template used in set_templateFeatureId template? – Jim Barntish Sep 23 '14 at 4:28

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.