Click here to Skip to main content
15,885,309 members
Articles / Programming Languages / Javascript

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Mar 2013CPOL 45.3K   2   4
A series of basic operations that can archive using JSOM

JSOM (JavaScript Client Object Model) is heavily used in SharePoint App model. I'm going to post a series of basic operations that can archive using JSOM.

First, add these references to your page:

JavaScript
//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 the following script to create the list:

JavaScript
<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 the 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIt did not work for me. Pin
mkamoski20-Dec-14 11:55
mkamoski20-Dec-14 11:55 
AnswerRe: It did not work for me. Pin
AlbertusV12-May-15 21:58
AlbertusV12-May-15 21:58 
Hi,
Although very late i only stumbled across the article yesterday.

Please check your use of Casing:

use SP.ListTemplateType.survey as supposed to SP.ListTemplateType.Survey

This is what I found to be the issue in my environment.

73
QuestionIn regards to the code... Pin
Member 1062792126-Feb-14 6:51
Member 1062792126-Feb-14 6:51 
QuestionWhere can i add ECMA script Pin
Sarathkumar Nallathamby19-Apr-13 2:06
professionalSarathkumar Nallathamby19-Apr-13 2:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.