Monday, October 19, 2009

Querying a SharePoint list with JavaScript

I came across an interesting article to Query SharePoint list Items using JavaScript which I want to share with you. JavaScript uses the built in web services of MOSS and makes HTTP Request to the web service to retrieve the list items.

Use the below script functions:

function QueryListEx(listGuid, fields, where, orderBy, rowLimit, extractRows)
{ var a = new ActiveXObject("Microsoft.XMLHTTP");
if(a == null) return null;
a.Open("POST", GetRootUrl() + "_vti_bin/DspSts.asmx", false);
a.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
a.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/dsp/queryRequest");
var d = '' + "" +" " +" " +" 1.0" +" " +" " +" " +" " + "" + "http://schemas.microsoft.com/sharepoint/dsp\">" +" " +" " +" " + fields + "" +" " + where + "" +" " + orderBy + "" +" " +" " +" " + "" + "";

a.Send(d);
if (a.status != 200)
return null;
else
{
if (extractRows)
return a.responseXML.selectNodes('//Row');
else
return a.responseXML;
}
}

function GetRootUrl()
{
var pathparts = document.location.pathname.split('/');
var url = 'https://' + document.location.hostname + '/' + pathparts[1] + '/';
return url;
}


function GetList(listName)
{
var a = new ActiveXObject("Microsoft.XMLHTTP");
if(a == null) return null;
a.Open("POST", GetRootUrl() + "_vti_bin/Lists.asmx", false);
a.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
a.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetList");
var d = "" + "" + "" + "http://schemas.microsoft.com/sharepoint/soap/\">" + "" + listName + "" + "" + "" + "";

a.Send(d);
if (a.status != 200)
return null;
else
return a.responseXML;
}

function GetListGuid(fullName)
{
var res = GetList(fullName);
if (res != null)
return res.selectSingleNode("//List").getAttribute("ID");
else
return null;
}

Calling QueryListEx to obtain results is a pretty easy process. A number of parameters are required:

listGuidThe internal guid of the list to be queried (see below).
fieldsAn XML string containing a list of fields to be returned by the query. The resultant fields will be represented in the output XML.
whereA query expression used to filter the rows from the list and create the result set. The following expression returns all records in the list where the field with the internal name of ID is equal to 1.
1
orderByAn XML fragement representing one or more fields to order the results by. Pass in an empty string to use the default sort order. The following example sorts by the Surname and Forename fields.
rowLimitSet to a positive integer to limit the size of the result set. It is always wise to restrict row sets to prevent potential performance problems or timeouts.
extractRowsSet this to true to automatically extract the results into an array of XML nodes. If set to false the entire XML DOM of the result will be returned.

1 comment:

  1. I have heard about another way of repair a pdf file. Besides, you can visit my blogs at: http://daspeac.livejournal.com/ or http://daspeac.blogspot.com/ where I’m trying to share my experience with regard to data corruption issues.

    ReplyDelete