List Operations In SharePoint 2016 Using REST API

SharePoint 2016 general availability had been announced in the Future Of SharePoint conference in May 2016. The series that discusses the installation of SharePoint 2016 in Azure can be found at C# Corner from the below links:

In this article we will see how to interact with List in SharePoint 2016 using REST API.The scope of the article is :

  • Create a new list
  • Fetch the properties of the list
  • Update the properties of the list
  • Delete the list

All of these operations will be carried out by making use of the corresponding REST API endpoint.

Create List using REST

We can create the item by issuing a POST request. The REST URL endpoint, used for the operation is:

/_api/web/Lists

We will be creating a key value pair of information, which will be used to create a list. The list creation information is shown below. It will be sent as JSON in the data attribute of the REST call.

  1. var metadata =  
  2.     {  
  3.     __metadata: {  
  4.         'type''SP.List'  
  5.     },  
  6.     BaseTemplate: 100,  
  7.     Description: 'This is a new SharePoint List created using REST',  
  8.     Title: 'REST List'  
  9. };  
The header for the POST request is as follows. It signifies the data type for the data submitted, as well the data type for the returned value.
  1. headers:   
  2.   
  3.     "accept""application/json;odata=verbose",  
  4.     "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  5.     "content-Type""application/json;odata=verbose"  
  6. },  
Output

Output

Output

Full Code
  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         var newListUrl = "/_api/web/Lists";  
  5.         var metadata = {  
  6.             __metadata: {  
  7.                 'type''SP.List'  
  8.             },  
  9.             BaseTemplate: 100,  
  10.             Description: 'This is a new SharePoint List created using REST',  
  11.             Title: 'REST List'  
  12.         };  
  13.         addNewItem(newListUrl, metadata)  
  14.     });  
  15.   
  16.     function addNewItem(newListUrl, metadata) {  
  17.         $.ajax({  
  18.             url: _spPageContextInfo.webAbsoluteUrl + newListUrl,  
  19.             type: "POST",  
  20.             headers: {  
  21.                 "accept""application/json;odata=verbose",  
  22.                 "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  23.                 "content-Type""application/json;odata=verbose"  
  24.             },  
  25.             data: JSON.stringify(metadata),  
  26.             success: function(data) {  
  27.                 console.log(data);  
  28.                 console.log("List created successfully.");  
  29.             },  
  30.             error: function(error) {  
  31.                 alert(JSON.stringify(error));  
  32.             }  
  33.         });  
  34.     }  
  35. </script>  
Get List Properties

The list properties can be retrieved by issuing the GET request, using the REST URL endpoint:

"/_api/Web/Lists/GetByTitle('REST List')

The header section for the GET request is plain and simple.
  1. headers:  
  2.   
  3.     "accept": "application/json;odata=verbose  
  4. },  
In the success callback, we can read the list properties from the returned object ‘data.d’ by indexing into it.

Output

Output

Full Code
  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         var newListUrl = "/_api/Web/Lists/GetByTitle('REST List')";  
  5.         getListProperties(newListUrl)  
  6.     });  
  7.   
  8.     function getListProperties(newListUrl) {  
  9.         $.ajax({  
  10.             url: _spPageContextInfo.webAbsoluteUrl + newListUrl,  
  11.             type: "GET",  
  12.             headers: {  
  13.                 "accept""application/json;odata=verbose",  
  14.             },  
  15.             success: function(data) {  
  16.                 console.log("List Description :" + data.d["Description"]);  
  17.                 console.log("EnableFolderCreation ? :" + data.d["EnableFolderCreation"]);  
  18.                 console.log("ListItemEntityTypeFullName : " + data.d["ListItemEntityTypeFullName"]);  
  19.             },  
  20.             error: function(error) {  
  21.                 alert(JSON.stringify(error));  
  22.             }  
  23.         });  
  24.     }  
  25. </script>  
Update List Properties

The list properties can be updated by issuing the MERGE call with the REST URL endpoint:

"/_api/Web/Lists/GetByTitle('REST List')”

The information to update can be assigned to the metadata key and assigned to the ‘data’ attribute of the rest call.
  1. var metadata =  
  2.     {  
  3.     __metadata: {  
  4.         'type''SP.Group'  
  5.     },  
  6.     Description: "This is an Updated Description"  
  7. };  
  8. updateList(newListUrl, metadata)  
  9. });  
The header section resembles what is given below:
  1. headers:  
  2.   
  3.     "accept""application/json;odata=verbose",  
  4.     "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  5.     "content-Type""application/json;odata=verbose",  
  6.     "IF-MATCH""*"  
  7. },  
Output

The success message has come up in the console .

Output

Checking the description of the list, we can see, that it has been updated.

description

Full Code
  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function()  
  4.     {  
  5.         var newListUrl = "/_api/Web/Lists/GetByTitle('REST List')";  
  6.         var metadata =  
  7.             {  
  8.             __metadata:  
  9.               {  
  10.                 'type''SP.Group'  
  11.             },  
  12.             Description: "This is an Updated Description"  
  13.         };  
  14.         updateList(newListUrl, metadata)  
  15.     });  
  16.   
  17.     function updateList(newListUrl, metadata) {  
  18.         $.ajax({  
  19.             url: _spPageContextInfo.webAbsoluteUrl + newListUrl,  
  20.             type: "MERGE",  
  21.             data: JSON.stringify(metadata),  
  22.             headers: {  
  23.                 "accept""application/json;odata=verbose",  
  24.                 "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  25.                 "content-Type""application/json;odata=verbose",  
  26.                 "IF-MATCH""*"  
  27.             },  
  28.             success: function(data) {  
  29.                 console.log("List description updated.");  
  30.             },  
  31.             error: function(error) {  
  32.                 alert(JSON.stringify(error));  
  33.             }  
  34.         });  
  35.     }  
  36. </script>  
Delete List

In order to delete the list, we can issue a Delete request to the Server, using the REST URL endpoint:

"/_api/Web/Lists/GetByTitle('REST List')

‘GetByTitle’ method specifies the list 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 header section is shown below:
  1. headers:  
  2.   
  3.     "accept""application/json;odata=verbose",  
  4.     "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  5.     "IF-MATCH""*"  
  6. },  
Full Code
  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function()  
  4.     {  
  5.         var existingListUrl = "/_api/Web/Lists/GetByTitle('REST List')";  
  6.         deleteList(existingListUrl);  
  7.     });  
  8.   
  9.     function deleteList(existingListUrl) {  
  10.         $.ajax({  
  11.             url: _spPageContextInfo.webAbsoluteUrl + existingListUrl,  
  12.             type: "DELETE",  
  13.             headers: {  
  14.                 "accept""application/json;odata=verbose",  
  15.                 "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  16.                 "IF-MATCH""*"  
  17.             },  
  18.             success: function(data) {  
  19.                 console.log("The list has been successfully deleted.");  
  20.             },  
  21.             error: function(error) {  
  22.                 alert(JSON.stringify(error));  
  23.             }  
  24.         });  
  25.     }  
  26. </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 the SharePoint page and click Web part from the Insert tab.

    edit settings

  • Add Content Editor Web part.

    Add Content Editor

  • Click Edit Web art from Content Edit Web part. Assign the URL of the script text file and click apply.

     Content Edit

  • Click Apply and we can see the successful list deletion message from the console.

    Apply

Upon refreshing the earlier list page, we will get the list not found error, which indicates the deletion of the list.

error

Conclusion

Thus, we have seen how to work with the lists and perform List creation, fetching & updating of its properties and finally deletion of the created list, using REST API in SharePoint 2016.