Home > SharePoint 2013 > SharePoint 2013 – Read List Items using REST API

SharePoint 2013 – Read List Items using REST API

SharePoint 2013 introduced Rich REST endpoints, we can use this to query data asynchronously using jQuery/JavaScript.

Here is a sample code which can be used to read items from a SharePoint list

var siteUrl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;

jQuery.ajax({
	url: siteUrl + "/_api/lists/getbytitle('ListTitle')/Items?$select Title",
	type: "GET",    
	headers: { 
		"accept": "application/json;odata=verbose",
		"content-type":"application/json;odata=verbose",
		"X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
	},
	success: function(d) {
		var stringData = JSON.stringify(d);
		var jsonObject = JSON.parse(stringData);
		var results = jsonObject.d.results;
		for(i = 0; i < results.length;i++) {
			console.log(results[i]["Title"])
		}
	},
	error: function() {
		console.log('fail');
	}
});

We are dynamically forming the site url using various tokens.

First we need to get the SharePoint list, for that we use REST method GetByTitle and pass the list title. Then we are selecting the item Title. You can add more fields by separating with comma.

The result is then converted to JSON object using JSON.Parse method.

Finally a loop is used to iterate through the items.

Advertisements
Categories: SharePoint 2013 Tags: , , ,
  1. December 23, 2014 at 10:29 PM

    Reblogged this on El Blog del Programador and commented:
    Aquí tenemos otro interesante artículo sobre cómo utilizar los servicios REST para leer listas de SharePoint. En un post anterior veíamos el acceso a listas de SharePoint 2013 a través de la API de cliente y AngularJS, el ejemplo de hoy utiliza JQuery y las opciones de Ajax de esta librería. Un saludo. Espero ya, poder dedicar algo de tiempo a hacer un ejemplo usando todo esto que vemos aquí.

  1. No trackbacks yet.

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

%d bloggers like this: