Skip to main content

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


function retriveListItem() 

{ 
03.   
04.    $.ajax 
05.    ({ 
06.        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('companyInfo')/items?$select=Company,Industry", 
07.        type: type, 
08.        data: data, 
09.        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.            $("#ResultDiv").empty(); 
21.            for (var i = 0; i < data.d.results.length; i++)  
22.            { 
23.                var item = data.d.results[i]; 
24.                $("#ResultDiv").append(item.Company + "\t" + item.Industry + "<br/>"); 
25.            } 
26.        }, 
27.        error: function(data) 
28.        { 
29.            $("#ResultDiv").empty().text(data.responseJSON.error); 
30.        } 
31.    }); 


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.    }); 

Comments

Popular posts from this blog

Ghosting and Unghosting

Ghosting means process of requesting an uncustomized page instance by using a page template loaded into memory from the file system of the front-end Web server. These Ghosted pages are pulled from the cache at runtime and therefore it will increase the scalability from the system. All uncustomized pages are reused across all the sites and there is no unnecessary data storage or retrieval. Ghosting eliminates the need to transfer the contents of a page definition file from the SQL Server computer with the content database to the front-end Web server computer For example, the default home page is a ghosted page. Any web part pages created via New Web Part Page user interface also ghosted. Unghosting means that the site has been customized. When you customize a site in SharePoint Designer, or you add custom fields to a Document Library, or create sites using that template; then the changes that you made are stored in the database as a difference and that is referred to as Unghost...