Wednesday, July 12, 2017

Rest API Curd Operations

Rest API Curd Operations

Reference urls

https://www.codeproject.com/Articles/990131/CRUD-operation-to-list-using-SharePoint-Rest-API
https://social.technet.microsoft.com/wiki/contents/articles/33795.sharepoint-2013-crud-operation-on-list-items-using-rest-api-services.aspx

Retrive LIst Items

   
    $.ajax 
    ({ 
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Testing')/items?$select=Title", 
       type: "GET", 
     //   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)  
        { 
debugger;
alert("Hiiiii");
            $("#ResultDiv").empty(); 
            for (var i = 0; i < data.d.results.length; i++)  
            { 
                var item = data.d.results[i]; 
                $("#ResultDiv").append(item.Title); 

            } 
        }, 
        error: function(data) 
        { 
alert("Hi");
            $("#ResultDiv").empty().text(data.responseJSON.error); 
        } 
    }); 
}



Create List Item


function AddListItem() 

02.

03.    var industryVal = $("#Industry").val(); 
04.    var Company = $("#Company").val(); 
05.    $.ajax 
06.        ({ 
07.        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items", 
08.        type: "POST", 
09.        data: JSON.stringify 
10.        ({ 
11.            __metadata: 
12.            
13.                type: "SP.Data.TestListItem" 
14.            }, 
15.            Company: Company, 
16.            Industry: industryVal 
17.        }), 
18.        headers: 
19.        
20.            "Accept": "application/json;odata=verbose", 
21.            "Content-Type": "application/json;odata=verbose", 
22.            "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
23.            "X-HTTP-Method": "POST" 
24.        }, 
25.        success: function(data, status, xhr) 
26.        
27.            retriveListItem(); 
28.        }, 
29.        error: function(xhr, status, error) 
30.        
31.            $("#ResultDiv").empty().text(data.responseJSON.error); 
32.        
33.    }); 
34.}



Update List Item



function updateListItem() 

02.

03.    var industryVal = $("#Industry").val(); 
04.    $.ajax 
05.    ({ 
06.        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items(7)", // list item ID 
07.        type: "POST", 
08.        data: JSON.stringify 
09.        ({ 
10.            __metadata: 
11.            
12.                type: "SP.Data.TestListItem" 
13.            }, 
14.            Industry: industryVal 
15.        }), 
16.        headers: 
17.        
18.            "Accept": "application/json;odata=verbose", 
19.            "Content-Type": "application/json;odata=verbose", 
20.            "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
21.            "IF-MATCH": "*", 
22.            "X-HTTP-Method": "MERGE" 
23.        }, 
24.        success: function(data, status, xhr) 
25.        
26.            retriveListItem(); 
27.        }, 
28.        error: function(xhr, status, error) 
29.        
30.            $("#ResultDiv").empty().text(data.responseJSON.error); 
31.        
32.    });

Delete List Item

function deleteListItem() 

02.

03.    $.ajax 
04.    ({ 
05.        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items(7)", 
06.        type: "POST", 
07.        headers: 
08.        
09.            "X-RequestDigest": $("#__REQUESTDIGEST").val(), 
10.            "IF-MATCH": "*", 
11.            "X-HTTP-Method": "DELETE" 
12.        }, 
13.        success: function(data, status, xhr) 
14.        
15.            retriveListItem(); 
16.        }, 
17.        error: function(xhr, status, error) 
18.        
19.            $("#ResultDiv").empty().text(data.responseJSON.error); 
20.        
21.    }); 

No comments: