Skip to main content

How to automate creation of list in SharePoint 2013 in different ways ?

Sharada Harnal, SharePoint Tech Lead at Trigent Software Ltd.
Sharada Harnal, SharePoint Tech Lead at Trigent Software Ltd.

SharePoint lists are easy to create by any IT Pro User without any assistance of a developer. List is often the primary source of data for solutions and apps. They can be easily accessed through API’s and support full CRUD (create, read, update and delete) operations. However, there are often scenarios where it makes more sense to automate the creation of lists by using a SharePoint solution. 

Declarative

New lists can be created in solutions or apps using a feature that contains a ListInstance element. The example below will create a new picture library for storing Christmas pictures

<ListInstance
TemplateType="109"
FeatureId = "D54C55BC-5922-4bb9-B187-1F06732B9072"
Title="Christmas Pictures"
Description="Trigent Christmas Picture"
Url="List/ ChristmasPictures"
OnQuickLaunch="TRUE" >
</ListInstance>

 

Pros:

Declarative approach is simple as it uses tools integrates with Visual Studio 2012.

Cons:

No graceful way to handle conflicts. For example, if we try to activate a feature to create a list with the same title as an already existing one, the feature activates successfully, but the feature’s attempt to create a list silently fails due to the conflict. For apps, this is not a problem because each app instance can create its own set of lists.

Code-Based

Depending on the requirement, we can use any of the below three approaches for creating lists using code

Pros:

  • More control when creating list programmatically, we can query the list collection of the current site to check if there is an existing list with the same title before attempting to create a new list.
  • More control over configuring properties when compared to declarative. This is to support attachments and allow versioning.

Server Side Object Model:

When we can create Farm solutions, mostly for on premises implementation, the solution must be physically deployed on the server.

SPWeb site= SPContext.Current.Web;
string ListTitle = "Christmas Pictures”;
string ListDescription = "Trigent Christmas Pictures”;
Guid ListId=site.Lists.Add(ListTitle, ListDescription, SPListTemplateType.PictureLibrary);
SPList list = site.Lists[ListId];
List.OnQuickLaunch = true;
List.Update();

Client Side Object Model:

Provides access to SharePoint content without installing code on the server that runs Microsoft SharePoint Foundation

var ctx = new SP.ClientContext.get_current();
var createInfo =  new ListCreateInofrmation();
createInfo.set_title(Christmas Pictures);
createInfo.set_templatetype( SPListTemplateType.PictureLibrary);
createInfo.set_description(Trigent Christmas Pictures);
var newList = ctx.get_web().get_lists().add(createInfo);
ctx.Load(newList);
ctx.executeQueryAsync(success, failure);

REST Interface:

Allows you to integrate SharePoint capabilities into code that runs remotely in client-side or server-side applications that run on computers where SharePoint 2013 has not been installed

$.ajax({
url: _spPageContextInfo.webServerRelativeUrl +
       "/_api/web/lists",
Type: "POST",
contentType: "application/json;odata=verbos",
data: JSON.tringify(
         { '_metadata' : {'type' : 'SP.List'},
'Title': 'Christmas Pictures',
'BaseTemplate': 109
})'
Headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#_REQUESTDIGEST").val()
}
        });