SharePoint 2010: How to Read SharePoint List using SharePoint REST API and JSON

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”.

1

The next step is to visit the Project Structure for this demonstration.

Project Structure:

2

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.

3

Definition of SharePointListInJASON.aspx

First of all we need to create a link which on click triggers the function “getListItems”.

4

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:

  • Parse the JSON response into a set of objects by using the JavaScript eval() function
  • Loop through the object to retrieve the required values
  • Prepare Html markup based on the retrieved values and display it on the page.

5

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:

6

Click the link and see the list items coming as a part of the response.

7

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:

8

If we inspect the different attributes of this request we find the following important information about this asynchronous service call:

9

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:

10

That’s the end of this simple implementation employing SharePoint Rest API and JSON.

Hope this will help someone in need…

Advertisements

2 thoughts on “SharePoint 2010: How to Read SharePoint List using SharePoint REST API and JSON

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s