CRUD operation on ListItem using REST

In the previous post we have seen CRUD operation on SharePoint List. In this post we are going to discuss about the operations on listitems using rest api.

Starting with the creation of Items class in our"RESTApiHelper" namespace.

RESTApiHelper.Items = function(SiteUrl,ListName){
    this.SiteUrl = SiteUrl;
    this.ListName = ListName;


Get All List Items in SharePoint List
   
    this.GetAllItems = function (doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/items",
            type: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: doSuccess,
            error: doError
        });
    }



Click here to see the sample response.

Get Items using url filter


    this.GetItems = function (select, filter, doSuccess, doError) {
        var urlfilter = "";
        if (select != '') {
            urlfilter = "?$select=" + select;
        }
        if (filter != '') {
            if (urlfilter != '')
                urlfilter = urlfilter + "&$filter=" + filter;
            else
                urlfilter = "?$filter=" + filter;
        }

        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/items" + urlfilter,
            type: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: doSuccess,
            error: doError
        });
    }
Get Items using CAML Query

    this.GetItemsByCaml = function (ViewXML, OnSuccess, OnError) {
        var serviceUrl = this.SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/getitems";

        var metadata = JSON.stringify({ 'query': { '__metadata': { 'type': 'SP.CamlQuery' }, 'ViewXml': ViewXML } });

        jQuery.ajax({
            url: serviceUrl,
            type: "POST",
            data: metadata,
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "Accept": "application/json; odata=verbose",
                "content-type": "application/json; odata=verbose",
                "content-length": metadata.length
            },
            success: OnSuccess,
            error: OnError
        });
    }

Get Item By ID

    this.GetItemByID = function (ItemId, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/getitembyid(" + ItemId + ")",
            type: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: doSuccess,
            error: doError
        });
    }

Create List Item

    this.AddItem = function (title, doSuccess, doError) {
        jQuery.ajax({
            url: this.SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/items",
            type: "POST",
            data: JSON.stringify({ '__metadata': { 'type': 'SP.Data.TestListListItem' }, 'Title': title }),
            headers: {
                "accept": "application/json;odata=verbose",
                "content-type": "application/json; odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: doSuccess,
            error: doError
        });

    }


Attachments: https://bloggerbrij.blogspot.com/2015/02/crud-operation-on-files-using-rest.html

Click here to get the full script. This is it for the list item object. In the next post we will work on files. So stay tuned and enjoy, have fun with REST and JQuery !!!!

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

Installation and Configuration of Office Online Server with SharePoint 2019

Workflow Manager 1.0- Installation, Configuration and Issues

SharePoint 2019 Upgrade Part 6: SharePoint Upgrade Issues