In this article we will see how JSON can be leveraged to access the SharePoint Lists using SharePoint REST API.
In order to execute this article we would need a SharePoint List which can be accessed using REST API later on.
For this demonstration we have a list by the name “LOB”, with one of the list columns by the name “LOB”.
The next step is to visit the Project Structure for this demonstration.
Based on the above project structure following are required artifacts to be developed in order to see JSON in action:
- SharePoint Mapped Folder: GetSharePointListInJASON
- Application Page : SharePointListInJASON.aspx
In order to develop this functionality we need to add the Application Page which will contain the logic to access SharePoint List using REST API and JSON.
Definition of SharePointListInJASON.aspx
First of all we need to create a link which on click triggers the function “getListItems”.
Now let’s dive deep into the execution of getListItems function.
getListItems function executes as follows:
- Specify the valid Url calling the REST Service
- Create a WebRequest Object using ASP.Net 3.5 Ajax API
- Specify the Request Type, in this case it is GET
- Specify the Accept Header which defines the data format of the Response, in this case it is JSON
- Specify the Callback function which will be executed by the runtime once the request is completed
onCompletedCallBack call back function executes as follows:
- Loop through the object to retrieve the required values
- Prepare Html markup based on the retrieved values and display it on the page.
And we are done.
Now build the solution and deploy it.
Navigate to the SharePointListInJASON.aspx Application Page which resides under layout directory in SharePoint Root (14 Hive) as shown below:
Click the link and see the list items coming as a part of the response.
Now let’s do some investigation on some of the internals of this asynchronous service call by using a Web Proxy Tool known as Fiddler:
If we follow the Fiddler, we can see traffic capture for this asynchronous service call as shown below:
If we inspect the different attributes of this request we find the following important information about this asynchronous service call:
- Request Type is Get on the URL http://demo2010a:2013/_vti_bin/ListData.svc/LOB
- Source of the Request is http://demo2010a:2013/_layouts/GetSharePointListInJASON/SharePointListInJASON.aspx which is the Application Page we use in this demonstration
- Response data format is JSON
If we inspect the response object, we will find the incoming JSON structure as follows, this information is very important to understand how to use the response the response object in code. Properties exposed by the response object are highlighted as follows:
That’s the end of this simple implementation employing SharePoint Rest API and JSON.
Hope this will help someone in need…