Tuesday, March 5, 2013

How to Query SharePoint List Items Using REST and ECMA script which returns as JSON.

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

//Use the URL we already formulated

//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

//Run the web requests

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() + ")");
//Fomulate HTML to display results
var Column1 = "
Column1 : ";
var Title = "Title : ";
Items.d.results.length);Column1 += 'Title: ' + Items.d.results[0]['Content'];
Title += '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
Raw JSON response = 'Raw JSON response:' + response.get_responseData();
Column1 );

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.


How to pass a value across Approval Levels and Check First Approver is not same as Second Approver in OOB SharePoint Designer Approval Workflow.

Here in one of my requirement I need to pass a value which is entered in level one of approval task from to second level of approval task form. After a  lot of search I was not able to find anything. This is the way I have done it. I will also demonstrate a way to check first app rover is not same as second. These are the steps that be useful for someone looking for same :

Step 1: We need to take two workflow variable on the completion condition of level 1 approval as shown in the below screen shot :

 Through these condition we can get our required values in workflow variables.(Here ID14743  is any field you want to save in this variable  and Modified By contains name of the first app rover. )

Step 2: Now save these two values in two respective fields, in the Before a task is Assigned Status of level 2 approval (This is the key here if you will update these task fields, it will lead to execution of Workflow. So just set it as shown in below Screen Shot). It is compulsory to create two  Task fields to store these values.

No as we have these values in our Task List fields so we can use them in our Info path form(Which is our Task Form as well). Because Info path form are by default associated with Task List fields.

In the info path form in what ever field we want to show this value we need to bind that field with our  Assigned fields. So they will display data entered at first level of approval. I

Step 3: As we got these values in our Info Path form so we need to edit our second level Task Form using info path form client and use some basic info path modification techniques to get it done.
a) In the Approve button of info path we need to set a rule that current user can not be same as what value is in the FirstApprover field (which we have set in this field before a task is assigned so the first app-rover value is with us already).

 We need to set very same rule in Reassign task or any action in which we want to check that First Approver should not perform this action. Using the value of First Approver Field and with new value entered or current user. There is a very important point here to note that always save and return values in same format as Name or User ID or something.

Hope it help someone. Will attach compare Screen Shot for First/Second Approver later.