Skip to content
Amal Hashim
All posts
Snippet

SharePoint 2013 - Read List Items using REST API

Read items from a SharePoint 2013 list with the REST API and jQuery, selecting only the fields you need.

1 min read#JavaScript#SharePoint 2013#jQuery

SharePoint 2013 introduced rich REST endpoints. You can use them to query list data asynchronously with jQuery or plain JavaScript.

This sample reads items from a list:

js
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 (var i = 0; i < results.length; i++) {
			console.log(results[i]["Title"]);
		}
	},
	error: function () {
		console.log('fail');
	}
});

How it works:

  1. The site URL is built from the current page using _spPageContextInfo.
  2. getbytitle finds the list by its title.
  3. $select=Title returns only the Title field. Add more fields separated by commas.
  4. The result is converted to a JSON object with JSON.parse.
  5. A loop goes through the items in d.results.

Example

With a list called "Tasks", replace ListTitle with Tasks. The console then shows the title of every task, one per line.