Friday, August 16, 2013

How to read list items using REST in Sharepoint 2010


Here I am giving on simple example that will read data from a list using REST.
Earlier we need to use SPAPI for this task but we can do very easily with REST.

var queryUrl ="http://YourSitename/_vti_bin/ListData.svc/ListName";

$.ajax({
    url: queryUrl,
    accepts: {
        json: "application/json;odata=verbose"
    },
 dataType: "json",              //By Default it returns in xml formate
    method: "GET",
    success: onQuerySuccess,
    error: onQueryError
});

function onQuerySuccess(data) {
var i =parseInt(0);
    if (data) {
        $.each(data.d.results, function () {
            alert(data.d.results[i].Title);    // give your internal column name of list
i=parseInt(i)+parseInt(1);
        });
    }
}
function onQueryError(error) {
   alert("ee");
}


We can get any column’s value.
data.d.results[i].Title

Here “Title” is the name of column so you can change the column name and get your data.

Thanks



No comments:

Post a Comment