Ram Prasad Meenavalli

SharePoint 2013 JSOM: How to create a list instance using a custom list definition?

Published on 15-Jun-2016 | Comments
Code for creating lists from OOB List Definition | Custom List Definition

Using Default/OOB List Definitions

Below is the code to create a list instance on a SharePoint site using one of the existing (OOB) list definitions.

var clientContext = SP.ClientContext.get_current(); var oWebsite = clientContext.get_web(); var listCreationInfo = new SP.ListCreationInformation(); listCreationInfo.set_title('My Announcements List'); listCreationInfo.set_templateType(SP.ListTemplateType.announcements); this.oList = oWebsite.get_lists().add(listCreationInfo); clientContext.load(oList); clientContext.executeQueryAsync( Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed) );

The SP.ListTemplateType enumeration has the following List Definitions. Once can use the required template to create a list instance.
  • SP.ListTemplateType.GenericList
  • SP.ListTemplateType.DocumentLibrary
  • SP.ListTemplateType.Survey
  • SP.ListTemplateType.Announcements
  • SP.ListTemplateType.Contacts
  • SP.ListTemplateType.Events
  • SP.ListTemplateType.Tasks
  • SP.ListTemplateType.DiscussionBoard
  • SP.ListTemplateType.PictureLibrary
  • SP.ListTemplateType.DataSources
  • SP.ListTemplateType.XmlForm
  • SP.ListTemplateType.NoCodeWorkflows
  • SP.ListTemplateType.WorkflowProcess
  • SP.ListTemplateType.WebPageLibrary
  • SP.ListTemplateType.CustomGrid
  • SP.ListTemplateType.WorkflowHistory
  • SP.ListTemplateType.GanttTasks
  • SP.ListTemplateType.IssuesTracking

How to create a List Instance using Custom List Definitions

As the SP.ListTemplateType enumeration doesn't include the custom list definitions, we cannot use it when we want to create a list using custom list definitions. Instead, we have to fetch the custom list definitions details from the SharePoint Web and use that information for creating the list instance. Below is the code.

//Title of the custom list definition/template var LIST_TEMPLATE_TITLE = "MY_PROJECTS"; var ctx = SP.ClientContext.get_current(); //Get the details of the custom list definition. var projectsTmpl = ctx.get_web().get_listTemplates().getByName(LIST_TEMPLATE_TITLE); ctx.load(projectsTmpl); ctx.executeQueryAsync(function () { var projectslistCI, projectsList; projectslistCI = new SP.ListCreationInformation(); projectslistCI.set_title("My Upcoming Projects"); projectslistCI.set_templateFeatureId(projectsTmpl.get_featureId()); projectslistCI.set_templateType(projectsTmpl.get_listTemplateTypeKind()) projectsList = ctx.get_web().get_lists().add(projectslistCI); ctx.load(projectsList); //List created using custom list definition/tempalte. ctx.executeQueryAsync(function (sender, args) { },function(sender, args){ //ERROR WHILE CREATING LISTS //LOG ERROR }); }, function (sender, args) { //ERROR WHILE FETCHING TEMPLATE DETAILS //LOG ERROR });


View Ram Prasad Meenavalli's LinkedIn profile

SharePoint Technology Evangelist