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:
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 = '' + "
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 = "" + "
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:
listGuid | The internal guid of the list to be queried (see below). |
fields | An XML string containing a list of fields to be returned by the query. The resultant fields will be represented in the output XML. |
where | A 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. |
orderBy | An 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. |
rowLimit | Set 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. |
extractRows | Set 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. |
No comments:
Post a Comment