CRUD Operation On List Items Using REST API Services In SharePoint 2013: Part Three

Before reading this article, please go through the following article series:

Introduction

SharePoint 2013 has greatly expanded the REST services available to developers. With this, we have much more SharePoint functionality exposed via JSOM and Web Services. The goal of this article is to provide how to perform basic create, read, update, and delete (CRUD) operations on lists and list items with the REST services.

SharePoint REST endpoint Overview

The following table contains typical REST endpoint URL examples to get you started working with SharePoint data. Prepend https://server/site/_api/ to the URL fragments shown in the table to construct a fully qualified REST URL. Below is a list of the basic commands used to get List Items from a SharePoint List through the SharePoint 2013 REST Services.

URL endpoint Description Supported HTTP Method
/_api/Web/Lists/ getbytitle('listname') Getting a list details by its title and updating it as well. Ifanyone changes your list title, your code will break. GET, POST
/_api/Web/Lists(guid'guid id of your list') Same as above but changing list title will not affect the code. GET, POST
/_api/Web/Lists/getbytitle(' listname ')/Fields Retrieving all fields associated with a list and add new fields GET, POST
/_api/Web/Lists/getbytitle('listname')/
Fields/getbytitle('fieldname')
Getting details of a field, modifying and deleting it. GET, PUT, PATCH, MERGE, DELETE
/_api/Web/Lists/getbytitle('listname')

/Items

Retrieving all items in a list and adding new items GET, POST
/_api/web/lists/getbytitle('listname')
/GetItemById(itemId)
 
This endpoint can be used to get, update and delete a single item. GET, PUT, PATCH, MERGE, DELETE
/_api/lists/ getbytitle (‘'listname')/items?$orderby=Title Order Your Results GET, POST
/_api/lists/ getbytitle (‘'listname')/items?$select=Title,Id Retrieve Selected Column Data value GET, POST
/_api/web/lists/getbytitle('listname')/Items/
?$select=Title,FieldName/Id&$expand= FieldName /Id
Retrieving the lookup value GET, POST

Now, I will demo all the operations on list items, including retrieve, create, update and delete on list items.

list

Retrieve the list items

item

Here is the main code in detail:

function retriveListItem()  
{  
  
    $.ajax  
    ({  
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items?$select=Company,Industry",  
        type: type,  
        data: data,  
        headers:  
        {  
            "Accept": "application/json;odata=verbose",  
            "Content-Type": "application/json;odata=verbose",  
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
            "IF-MATCH": "*",  
            "X-HTTP-Method": null  
        },  
        cache: false,  
        success: function(data)   
        {  
            $("#ResultDiv").empty();  
            for (var i = 0; i < data.d.results.length; i++)   
            {  
                var item = data.d.results[i];  
                $("#ResultDiv").append(item.Company + "\t" + item.Industry + "<br/>");  
            }  
        },  
        error: function(data)  
        {  
            $("#ResultDiv").empty().text(data.responseJSON.error);  
        }  
    });  
}

Create list item

create

Here is the main code in detail: 

function AddListItem()  
{  
    var industryVal = $("#Industry").val();  
    var Company = $("#Company").val();  
    $.ajax  
        ({  
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items",  
        type: "POST",  
        data: JSON.stringify  
        ({  
            __metadata:  
            {  
                type: "SP.Data.TestListItem"  
            },  
            Company: Company,  
            Industry: industryVal  
        }),  
        headers:  
        {  
            "Accept": "application/json;odata=verbose",  
            "Content-Type": "application/json;odata=verbose",  
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
            "X-HTTP-Method": "POST"  
        },  
        success: function(data, status, xhr)  
        {  
            retriveListItem();  
        },  
        error: function(xhr, status, error)  
        {  
            $("#ResultDiv").empty().text(data.responseJSON.error);  
        }  
    });  
} 

Update list item

update

Here is the main code in detail: 

function updateListItem()  
{  
    var industryVal = $("#Industry").val();  
    $.ajax  
    ({  
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items(7)", // list item ID  
        type: "POST",  
        data: JSON.stringify  
        ({  
            __metadata:  
            {  
                type: "SP.Data.TestListItem"  
            },  
            Industry: industryVal  
        }),  
        headers:  
        {  
            "Accept": "application/json;odata=verbose",  
            "Content-Type": "application/json;odata=verbose",  
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
            "IF-MATCH": "*",  
            "X-HTTP-Method": "MERGE"  
        },  
        success: function(data, status, xhr)  
        {  
            retriveListItem();  
        },  
        error: function(xhr, status, error)  
        {  
            $("#ResultDiv").empty().text(data.responseJSON.error);  
        }  
    }); 

Delete list item

delete

Here is the main code in detail:  

function deleteListItem()  
{  
    $.ajax  
    ({  
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items(7)",  
        type: "POST",  
        headers:  
        {  
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
            "IF-MATCH": "*",  
            "X-HTTP-Method": "DELETE"  
        },  
        success: function(data, status, xhr)  
        {  
            retriveListItem();  
        },  
        error: function(xhr, status, error)  
        {  
            $("#ResultDiv").empty().text(data.responseJSON.error);  
        }  
    });  
}

Summary

In this article, we explored SharePoint 2013 REST API for (CRUD) operations on list items level. I’ve tried to explore crud operation using REST Services, JavaScript Client Side Object Model, and SOAP Services to work on the client side.

Read more articles on SharePoint: