Monday, March 11, 2013

Create a SharePoint 2013 List Using JSOM (ECMAScript Client Object Model)

JSOM (JavaScript client object model) heavily used in SharePoint App model. I'm going to post series of basic operations that can archive using jsom.

First add these references to your page.

//Add jquery version correctly   
<script type="text/javascript" src="../Scripts/jquery-1.7.1.min.js"></script>

<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/init.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>




Then add following script to create the list.


<script type="text/ecmascript">

function createList() {
var clientContext = new SP.ClientContext.get_current();
var oWebsite = clientContext.get_web();

var listCreationInfo = new SP.ListCreationInformation();
listCreationInfo.set_title('CustomList'); // list name
listCreationInfo.set_description('description'); // list description
listCreationInfo.set_templateType(SP.ListTemplateType.genericList); //list type

oWebsite.get_lists().add(listCreationInfo);

clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),// when success
Function.createDelegate(this, this.onQueryFailed) // when failed
);
}
function onQuerySucceeded() {
alert("List Created");
}

function onQueryFailed(sender, args) {
alert("List Failed");
}

</script>


When creating a list you can use following templates


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

Post a Comment