Here is the way you can query data from a SharePoint List by using REST and then return all these items in JSON format. You need to call these two methods with all required values. Here I am going to query a list in which "Title" column contains value 'CR' :
This method you can call in inn your any event, where you want to access data of a SP list through REST in JSON format :
function getListItems() {
var Url = "http://<servername>:<portno>/sites/<sitename>/_vti_bin/listdata.svc/<ListName>?$filter=substringof('CR',Title)";
//Create a WebRequest object
var request = new Sys.Net.WebRequest();
//Specify the verb
request.set_httpVerb("GET");
//Use the URL we already formulated
request.set_url(Url);
//Set the Accept header to ensure we get a JSON response
request.get_headers()["Accept"] = "application/json";
//Add a callback function that will execute when the request is completed
request.add_completed(onCompletedCallback);
//Run the web requests
request.invoke();
}
This function runs when the web request completes so make sure you will get response.
function onCompletedCallback(response, eventArgs) {
//Parse the JSON reponse into a set of objects by using the JavaScript eval() function
var Items= eval("(" + response.get_responseData() + ")");
alert(Items);
//Fomulate HTML to display results
var Column1 = "Column1 : ";
var Title = "Title : ";
alert(Items.d.results.length);Column1 += 'Title: ' + Items.d.results[0]['Content'];
Title += 'Title: ' + Items.d.results[0]['Title'];
//var IDs= "IDList : ";
//var Titles = "TitleList : ";
//for (var i = 0; i < Items.d.results.length; i++) {
//Display some properties
//Titles += 'Title: ' + Items.d.results[i]Title + ";
//IDs += 'ID: ' + Items.d.results[i].Id + ";
//}
//Display the raw JSON response
var Raw JSON response = 'Raw JSON response:' + response.get_responseData();
alert(Column1 );
alert(Title);
}
So by using this code you can get response from a list without creating so many objects(e.g. : Web, site, list, Caml Query and many more). These three alert will show Raw JSON response, Column(which can be any) and Title. Which you can customize further for use.
* Highlight 1: Its a contain filter, you can apply any as per your need. There is a very rich set of keywords there to filter.
* Highlight2: You can un comment this code and it will give you collection of items.
This method you can call in inn your any event, where you want to access data of a SP list through REST in JSON format :
function getListItems() {
var Url = "http://<servername>:<portno>/sites/<sitename>/_vti_bin/listdata.svc/<ListName>?$filter=substringof('CR',Title)";
//Create a WebRequest object
var request = new Sys.Net.WebRequest();
//Specify the verb
request.set_httpVerb("GET");
//Use the URL we already formulated
request.set_url(Url);
//Set the Accept header to ensure we get a JSON response
request.get_headers()["Accept"] = "application/json";
//Add a callback function that will execute when the request is completed
request.add_completed(onCompletedCallback);
//Run the web requests
request.invoke();
}
This function runs when the web request completes so make sure you will get response.
function onCompletedCallback(response, eventArgs) {
//Parse the JSON reponse into a set of objects by using the JavaScript eval() function
var Items= eval("(" + response.get_responseData() + ")");
alert(Items);
//Fomulate HTML to display results
var Column1 = "Column1 : ";
var Title = "Title : ";
alert(Items.d.results.length);Column1 += 'Title: ' + Items.d.results[0]['Content'];
Title += 'Title: ' + Items.d.results[0]['Title'];
//var IDs= "IDList : ";
//var Titles = "TitleList : ";
//for (var i = 0; i < Items.d.results.length; i++) {
//Display some properties
//Titles += 'Title: ' + Items.d.results[i]Title + ";
//IDs += 'ID: ' + Items.d.results[i].Id + ";
//}
//Display the raw JSON response
var Raw JSON response = 'Raw JSON response:' + response.get_responseData();
alert(Column1 );
alert(Title);
}
So by using this code you can get response from a list without creating so many objects(e.g. : Web, site, list, Caml Query and many more). These three alert will show Raw JSON response, Column(which can be any) and Title. Which you can customize further for use.
* Highlight 1: Its a contain filter, you can apply any as per your need. There is a very rich set of keywords there to filter.
* Highlight2: You can un comment this code and it will give you collection of items.
No comments:
Post a Comment