Create an Item in a List in SharePoint 2013 Using CSOM-JavaScript

Introduction

This article explains how to create an item in a list in SharePoint 2013 using CSOM-JavaScript.

Prerequisites

The following are the prerequisites:

  1. Ensure you have access to the Office 365 online.
  2. Ensure Napa tool is available in your site.

Procedure

The following is the procedure to be followed:

  1. Create an App For SharePoint Using Office 365 Tools. If you have missed out on how to create an app in SharePoint 2013, then Click here.
  2. Create a list and name it “MyCustomList”. Click here if you want to learn how to create a list.
  3. Create a field in the list. Click here if you want to learn how to create a field in a list.
  4. Click on the "Default.aspx" page.

    aspx page

  5. Add Title and MyCustomField text boxes and a button to create an item and place it inside the “PlaceHolderMain” tag.
     

    Title : <input type="text" name="txtTitle" id="txtTitle"></input><br/><br/><br/>

    MyCustomField : <input type="text" name="txtCustom" id="txtCustom"></input><br/><br/><br/>

    <button id="btnCreateItem" onclick="createItem()">Create a New item</button><br/>

    create an item
     

  6. Click on the "App.js" file.

    js file

  7. Globally declare the content, web and list objects as shown below.

    var context = SP.ClientContext.get_current(); //gets the current context

    var web = context.get_web(); //gets the web object

    var list = web.get_lists(); //gets the collection of lists
     

  8. Now write the function to create an item in a list as in the following:
     

        function createItem() {

            // Retrieve the list and add an item to it.

            var targetList = list.getByTitle("MyCustomList");

            //create a new item object

            var listItemCreation = new SP.ListItemCreationInformation();

            //add the item to the list using addItem method

            var newItem = targetList.addItem(listItemCreation);

            var listItemTitle = document.getElementById("txtTitle").value;

            alert(listItemTitle);

            var listItemCustom = document.getElementById("txtCustom").value;

            alert(listItemCustom);

            //using set_item method u can set the values to the item fields

            newItem.set_item('Title', listItemTitle);

            newItem.set_item('MyCustomField', listItemCustom);

            newItem.update();

            context.load(newItem);

            context.executeQueryAsync(ItemCreationSuccess, ItemCreationFail);

        }
     

  9. Here in this example we are creating an item in a list using the “ListItemCreationInformation” object.
  10. After creating the listitem object we need to add the object to the list using the additems method.
  11. Then set the values of the item field using the set_item method.
  12. Then we need to load the new item object and execute the code by calling "executeQueryAsync".
     

    function ItemCreationSuccess() {

            $('#message').text = "";

            $('#message').text("Item got created");

        }

        function ItemCreationFail(sender, args) {

            alert('Failed to create a new item. Error:' + args.get_message());

        }
     

  13. That’s it! Now let's start testing.

Testing

  1. Now to run the app click on the "Play" button available towards the left-most corner.

    Play button

  2. The app is packaged, deployed, and installed on your Office 365 Site.

    app

  3. Now you will be able to see the page where you can enter the details and store it in the list.

    page with enter detail

  4. Enter the details and click on the “create a new item” button.

    Enter the details

  5. To see the item that was recently created, just go to the following URL:

    Syntax: https://yoursite/Lists/ListName
    Example: https://mysite/Lists/MyCustomList

    item created

Summary

Thus in this article you saw how to create an item in a list in SharePoint 2013 using CSOM-JavaScript.