Tuesday, June 5, 2018

CRUD Operations using Rest API

To Retrieve the items

  1. function retriveListItem()  
  2. {  
  3.     var def = $.Deferred();
  4.     $.ajax  
  5.     ({  
  6.         url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('testlist')/items?$select=Company,Industry",  
  7.         type: type,  
  8.         data: data,  
  9.         headers:  
  10.         {  
  11.             "Accept""application/json;odata=verbose",  
  12.             "Content-Type""application/json;odata=verbose",  
  13.             "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  14.             "IF-MATCH""*",  
  15.             "X-HTTP-Method"null  
  16.         },  
  17.         cache: false,  
  18.         success: function(data)   
  19.         {  
  20.             def.resolve();
  21.             $("#ResultDiv").empty();  
  22.             for (var i = 0; i < data.d.results.length; i++)   
  23.             {  
  24.                 var item = data.d.results[i];  
  25.                 $("#ResultDiv").append(item.Company + "\t" + item.Industry + "<br/>");  
  26.             }  
  27.         },  
  28.         error: function(data)  
  29.         {  
  30.             $("#ResultDiv").empty().text(data.responseJSON.error);  
  31.         }  
  32.     });  
  33.     return def.promise();
  34. }  

To Create list item

  1. function AddListItem()  
  2. {  
  3.     var def = $.Deferred();
  4.     var industryVal = $("#Industry").val();  
  5.     var Company = $("#Company").val();  
  6.     $.ajax  
  7.         ({  
  8.         url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('testlist')/items",  
  9.         type: "POST",  
  10.         data: JSON.stringify  
  11.         ({  
  12.             __metadata:  
  13.             {  
  14.                 type: "SP.Data.TestListItem"  
  15.             },  
  16.             Company: Company,  
  17.             Industry: industryVal  
  18.         }),  
  19.         headers:  
  20.         {  
  21.             "Accept""application/json;odata=verbose",  
  22.             "Content-Type""application/json;odata=verbose",  
  23.             "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  24.             "X-HTTP-Method""POST"  
  25.         },  
  26.         success: function(data, status, xhr)  
  27.         {  
  28.             def.resolve();
  29.         },  
  30.         error: function(xhr, status, error)  
  31.         {  
  32.             $("#ResultDiv").empty().text(data.responseJSON.error);  
  33.         }  
  34.     });  
  35.     return def.promise();
  36. }  

To Update list item

  1. function updateListItem()  
  2. {  
  3.     var def = $.Deferred();
  4.     var industryVal = $("#Industry").val();  
  5.     $.ajax  
  6.     ({  
  7.         url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('testlist')/items(7)"// list item ID  
  8.         type: "POST",  
  9.         data: JSON.stringify  
  10.         ({  
  11.             __metadata:  
  12.             {  
  13.                 type: "SP.Data.TestListItem"  
  14.             },  
  15.             Industry: industryVal  
  16.         }),  
  17.         headers:  
  18.         {  
  19.             "Accept""application/json;odata=verbose",  
  20.             "Content-Type""application/json;odata=verbose",  
  21.             "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  22.             "IF-MATCH""*",  
  23.             "X-HTTP-Method""MERGE"  
  24.         },  
  25.         success: function(data, status, xhr)  
  26.         { 
  27.             def.resolve(); 
  28.         },  
  29.         error: function(xhr, status, error)  
  30.         {  
  31.             $("#ResultDiv").empty().text(data.responseJSON.error);  
  32.         }  
  33.     });  
  34.     return def.promise();
  35. }


To Delete list item

  1. function deleteListItem()  
  2. {  
  3.     var def = $.Deferred();
  4.     $.ajax  
  5.     ({  
  6.         url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('testlist')/items(7)",  
  7.         type: "POST",  
  8.         headers:  
  9.         {  
  10.             "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  11.             "IF-MATCH""*",  
  12.             "X-HTTP-Method""DELETE"  
  13.         },  
  14.         success: function(data, status, xhr)  
  15.         {  
  16.             def.resolve(); 
  17.         },  
  18.         error: function(xhr, status, error)  
  19.         {  
  20.             $("#ResultDiv").empty().text(data.responseJSON.error);  
  21.         }  
  22.     });  
  23.     return def.promise();
  24. }

Usage

 retriveListItem().then(function () {
            
        });

Wednesday, March 21, 2018

Monday, October 9, 2017

AJAX/Rest only returning 100 items instead of full list

The OData provider for the REST API is configured to limit the number of items in a response to 100. It protects against developers inadvertently making a request that returns large result sets.
If there are more items the response will contain a continuation. In a JSON response the continuation will be a property named __next.
You can use $top if you want to override this behavior and have a large result set to be returned.
var url = appUrl + "/_api/Web/Lists/getByTitle('Order Details')/Items";  

var message = jQuery("#message");
message.text("");
getItems();  

function getItems() {
    var call = jQuery.ajax({
        url: url,
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }
    });
    call.done(function (data, textStatus, jqXHR) {
        message.append(String.format("Retrieved {0} items",
            data.d.results.length));
        message.append("<br/>");

        if (data.d.__next) {
            url = data.d.__next;
            getItems();
        }
    });
    call.fail(failHandler);
}
or you can always use $top=2000 in query