SharePoint 2016 general availability had been announced in the Future Of SharePoint conference in May 2016. The series, which discusses the installation of SharePoint 2016 in Azure can be found at C# Corner from the links, given below:
In this article, we will see, how to interact with List Items, using REST API. As an initial prerequisite, ensure that we have a list of the name DepartmentWing created in the Site with the columns, as shown below:
We will use REST to connect to SharePoint to perform the list item operations.The scope of the article involves the operations, given below, using REST,
- Fetch all the List Items
- Create a new List Item
- Update existing List Item
- Delete existing List Item
Fetch List Items
In order to get all the list items from the List, we can issue a Get request to SharePoint List. The REST URL is used for the AJAX request will be of the format :
/_api/Web/Lists/GetByTitle('DepartmentWing')/Items
The method used to send the request will be a GET request. In the headers attribute, we specify the type of return data expected, which is JSON here.The result returned will be iterated in the success call back function.The header of the AJAX GET request will be plain and simple, as shown below:
- headers: {
- "accept": "application/json;odata=verbose",
- },
Output
We are trying to retrieve the list item and display the name and expertise of the resource, which is shown below in the console:
Full Code
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function()
- {
- var listUrl = "/_api/Web/Lists/GetByTitle('DepartmentWing')/Items";
- getItem(listUrl)
- });
-
- function getItem(listUrl) {
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl + listUrl,
- type: "GET",
- headers: {
- "accept": "application/json;odata=verbose",
- },
- success: function(data) {
- console.log(data.d.results);
- var items = data.d.results;
- for (var i = 0; i < items.length; i++) {
- console.log(items[i].Name + ":" + items[i].Expertise);
- }
- },
- error: function(error) {
- alert(JSON.stringify(error));
- }
- });
- }
- </script>
Create List Item
We can create the list item by issuing a POST AJAX request. The REST URL used for the operation is:
/_api/Web/Lists/GetByTitle('DepartmentWing')/Items
We will be creating a key value pair of information, which will be used to create the list. The list creation information property is shown below. It will be sent as JSON in the ‘data’ attribute of the AJAX REST call.
- var metadata =
- {
- __metadata:
- {
- 'type': 'SP.Data.DepartmentWingListItem'
- },
- Name: 'Priyaranjan KS',
- Expertise: 'SharePoint',
- Location: 'Delhi'
- };
Within the _metadata attribute, we have to specify the value for ListItenEntityTypeFullName('SP.Data.DepartmentWingListItem'). We can get this value by issuing a GET request in the Browser, using the URL, given below:
http://sharepoint2016/sites/HOL/_api/lists/getbytitle('DepartmentWing')?$select=ListItemEntityTypeFullName
The header section will look as below,
- headers:
- {
- "accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "content-Type": "application/json;odata=verbose"
- },
Here, accept attribute specifies the data type for the return value and the content-type defines the data type for the data sent to the Server. In POST request, we have to send the X-RequestDigest value along with the request for the form validation without which, we will get a validation error. To fulfil this, we will be assigning the $("#__REQUESTDIGEST").val(), which sets the value of the form digest control present within the page to X-RequestDigest key.
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function()
- {
- var newItemUrl = "/_api/Web/Lists/GetByTitle('DepartmentWing')/Items";
- var metadata =
- {
- __metadata:
- {
- 'type': 'SP.Data.DepartmentWingListItem'
- },
- Name: 'Priyaranjan KS',
- Expertise: 'SharePoint',
- Location: 'Delhi'
- };
- addNewItem(newItemUrl, metadata)
- });
-
- function addNewItem(newItemUrl, metadata) {
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl + newItemUrl,
- type: "POST",
- headers: {
- "accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "content-Type": "application/json;odata=verbose"
- },
- data: JSON.stringify(metadata),
- success: function(data) {
- console.log(data);
- },
- error: function(error) {
- alert(JSON.stringify(error));
- }
- });
- }
- </script>
Output: The console output looks, as given below:
A new item has been created in SharePoint List.
Update the list item
The list item can be updated by issuing AJAX MERGE call with the REST URL :
"/_api/Web/Lists/GetByTitle('DepartmentWing')/Items(1)";
The information to update the list item can be assigned to the metadata key and assigned to the ‘data’ attribute of AJAX call.
- var metadata = { __metadata: { 'type': 'SP.Data.DepartmentWingListItem' },Location : 'Kerala' };
ListItenEntityTypeFullName can be obtained, as mentioned in create Item operation. The header section looks similar to the previous operations.
- headers:
- {
- "accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "content-Type": "application/json;odata=verbose",
- "IF-MATCH": "*"
- }
Output : The list item column value for ‘Location’ has been updated, as shown below:
Full code
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function()
- {
- var existingItemUrl = "/_api/Web/Lists/GetByTitle('DepartmentWing')/Items(1)";
- var metadata = {
- __metadata: {
- 'type': 'SP.Data.DepartmentWingListItem'
- },
- Location: 'Kerala'
- };
- updateItem(existingItemUrl, metadata)
- });
-
- function updateItem(existingItemUrl, metadata) {
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl + existingItemUrl,
- type: "MERGE",
- data: JSON.stringify(metadata),
- headers: {
- "accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "content-Type": "application/json;odata=verbose",
- "IF-MATCH": "*"
- },
- success: function(data) {
- console.log(data);
- },
- error: function(error) {
- alert(JSON.stringify(error));
- }
- });
- }
- </script>
Delete List Item
In order to delete the list item, we can issue a AJAX Delete request to the Server, using REST URL :
/_api/Web/Lists/GetByTitle('DepartmentWing')/Items(1)
‘Items(1)’ specifies the item Id of the item to be deleted. In the header section, we can specify the ‘accept’ and ‘X-RequestDigest’ attribute , which specifies the return data type and the form digest value respectively. The “IF-Match” attribute is used to check the concurrency of the list item to ensure that the item, that is being deleted is really the one, which we intend to delete. We can either specify “*”, which will blindly skip the concurrency check, else we can specify the e-tag value (which can be obtained by issuing a GET request).
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function()
- {
- var existingItemUrl = "/_api/Web/Lists/GetByTitle('DepartmentWing')/Items(1)";
- deleteItem(existingItemUrl)
- });
-
- function deleteItem(existingItemUrl) {
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl + existingItemUrl,
- type: "DELETE",
- headers: {
- "accept": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "IF-MATCH": "*"
- },
- success: function(data) {
- console.log(data);
- },
- error: function(error) {
- alert(JSON.stringify(error));
- }
- });
- }
- </script>
Let’s see how we can implement it in SharePoint. Save the script as a text file and upload it to the site assets library.
SharePoint Implementation
- Go to the edit settings of SharePoint page and click Web part from the Insert tab.
- Add Content Editor Web part.
- Click Edit Web art from Content Edit Web part. Assign the URL of the script text file and click Apply.
- Click Apply and we can see the successful list deletion message from the console.
Output
Upon refreshing the list item’s display page, which was present in the Browser prior to deletion, we will get the list item, which has not found an error, which indicates the deletion of the list item.
Conclusion
Thus, we have seen, how to perform the list item operations in SharePoint 2016, using REST API. This has been tried and tested in Office 365 as well.