Wednesday 22 October 2014

REST Services CRUD Operations in Sharepoint 2013

REST Services – High Level Overview

Let’s start out with our basic get commands in REST. Below is a list of the basic commands used to get List Items from a SharePoint List through the SharePoint 2013 REST Services.

COMMAND

URL
Get All Lists
http://server/site/_api/lists
Get All List Items From a Single List
http://server/site/_api/lists/getbytitle(‘listname’)/items
Get a Single List Item
http://server/site/_api/lists/getbytitle(‘listname’)/items
Get Back Certain Columns
http://server/site/_api/lists/getbytitle(‘listname’)/items?$select=Title,Id
Order Your Results
 http://server/site/_api/lists/getbytitle(‘listname’)/items?$orderby=Title

CRUD operations using REST in Sharepoint 2013

Get a Single List Item

function getListItem(url, listname, id, complete, failure) 
{
        // Getting our list items
        $.ajax({
                url: url + "/_api/web/lists/getbytitle('" + listname + "')/items(" + id + ")",
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },
                success: function (data) {
                        // Returning the results
                        complete(data);
                },
                error: function (data) {
                        failure(data);
                }
                });
        }
}


Creating list items gets a little bit tricky, because you’ll need a few key pieces of information:
- The List Item type 
- REQUESTDIGEST value to prevent replay attacks
- An object containing your List Item Values
// Adding a list item with the metadata provided
function addListItem(url, listname, metadata, success, failure) 
{
    // Prepping our update
    var item = $.extend({
        "__metadata": { "type": getListItemType(listname)}
    }, metadata);

    // Executing our add
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            success(data); // Returns the newly created list item information
        },
        error: function (data) {
            failure(data);
        }
    });
}


Updating list items is very similar to the task of creating list items when using jQuery Ajax calls.  You’ll need the same information you used then you created your list item, with one more piece of information: 
- The List Item Type 
- REQUESTDIGEST 
- An object containing your List Item Values & Item Type 
- The REST URL to your List Item (We use our getListItem function to get this reliably, See Above)
- Stored in: data.d.__metadata.uri
function updateListItem(url, listname, id, metadata, success, failure)
{
    // Prepping our update
    var item = $.extend({
        "__metadata": { "type": getListItemType(listname) }
    }, metadata);

    getListItem(url, listname, id, function (data) {
        $.ajax({
            url: data.d.__metadata.uri,
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(item),
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "MERGE",
                "If-Match": data.d.__metadata.etag
            },
            success: function (data) {
                success(data);
            },
            error: function (data) {
                failure(data);
            }
        });

    }, function (data) {
        failure(data);
    });
}
Deleting a list item also requires the REST URL that leads to the List Item you would like to delete.  Again, we get this through our getListItem function (See Above).  So you’ll need: 
- The List Item Type 
- REQUESTDIGEST 
- REST URL to your List Item (via our getListItem function)
// Deleting a List Item based on the ID
function deleteListItem(url, listname, id, success, failure) 
{
    // getting our item to delete, then executing a delete once it's been returned
    getListItem(url, listname, id, function (data) {
        $.ajax({
            url: data.d.__metadata.uri,
            type: "POST",
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-Http-Method": "DELETE",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "If-Match": data.d.__metadata.etag
            },
            success: function (data) {
                success(data);
            },
            error: function (data) {
                failure(data);
            }
        });
    });
};

No comments:

Post a Comment