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

Accordion "Left Navigation" (Quick Launch) for SharePoint 2013

For Expand/collapse "Left Navigation (Quick Launch) for sharepoint 2013 Copy the below code and paste in Master page jQuery(document).ready(function () {  /*set dynamic css logic*/  if($('#sideNavBox .menu-item.selected').length){   //propagates the selected class, up the three.   $('li.static').removeClass('selected');   $('#sideNavBox .menu-item.selected').parents('li.static').addClass('selected');   //collapses top siblings of selected branch   $('#sideNavBox .menu-item.selected').parents('li.static').last().siblings()    .find('> ul').hide();  }  else $('#sideNavBox .root.static > li.static > ul').hide();  /*set accordion effect*/  $('#sideNavBox .root.static > li.static').each(function(){   if($(this).find('ul').length){    $(this).addClass('father').click(function(){     if($(this).children('ul').css('display')...

SharePoint 2013 - Suggested Content Browser Locations

In SharePoint Site collection, we can configure “Suggested Content Browser Locations” to access the directories easily. When we configure these locations, we can see them as drop down to the users when we select the content SharePoint. To view this link, we need to activate the publishing feature. We can configure suggested content browser location at site collection level. To configure suggested content browser locations, navigate to Site Settings, in the Site Collection Administration section, click on “Suggested Content Browser Locations” link. We can see the list. Click on new item to create new suggested location link. To check the suggested content browser location navigates to any page, edit the page. Select Insert option. In the Select Asset dialog, We can see the Suggested link drop down as shown the image below.