How to query list data using web service
In SharePoint, to query list data we can also use web services provided by SharePoint other than SharePoint API. We can call web services from two places one is JavaScript and another one is .Net client application. Here I will show you both methods.
.Net client application
To query list data we will use a web service called lists.asmx.
1. Create .net project either windows/console.
2. Add the web reference and the URL looks like http://<servername:portno>/_vti_bin/lists.asmx
3. Give the web reference name as ListsSvc.
3. Add the following code snippet to get list data.
ListsSvc.Lists listService = new ListsSvc.Lists();
// If the current user is having access you can use below code
listService.Credentials = CredentialCache.DefaultCredentials;
// In case if you want to pass specific user credentials
listService.Credentials = new NetworkCredential("<username>", "<password>", "<domain>");
string viewName = "{03D22CAA-6534-49B4-9975-F101BFBCE2F4}"; //your view GUID
XmlNode ndListItems = listService.GetListItems("<list name>", viewName, null, null, "100", null, null);
.Net client application
To query list data we will use a web service called lists.asmx.
1. Create .net project either windows/console.
2. Add the web reference and the URL looks like http://<servername:portno>/_vti_bin/lists.asmx
3. Give the web reference name as ListsSvc.
3. Add the following code snippet to get list data.
ListsSvc.Lists listService = new ListsSvc.Lists();
// If the current user is having access you can use below code
listService.Credentials = CredentialCache.DefaultCredentials;
// In case if you want to pass specific user credentials
listService.Credentials = new NetworkCredential("<username>", "<password>", "<domain>");
string viewName = "{03D22CAA-6534-49B4-9975-F101BFBCE2F4}"; //your view GUID
XmlNode ndListItems = listService.GetListItems("<list name>", viewName, null, null, "100", null, null);
foreach (XmlNode node in ndListItems)
{
if (node.Name == "rs:data")
{
for (int i = 0;
i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Name == "z:row")
{
Console.Write(node.ChildNodes[i].Attributes["ows_Country"].Value);
Console.Write(node.ChildNodes[i].Attributes["ows_Regions"].Value);
}
}
}
}
JavaScript
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var soapEnv =
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
<soapenv:Body> \
<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
<listName>Countries</listName> \
<viewFields> \
<ViewFields><FieldRef Name='Title' /></ViewFields> \
</viewFields> \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>";
$.ajax({
url: "http://<server name>/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processListAccessResult,
contentType: "text/xml; charset=\"utf-8\""
});
});
// Process result
function processListAccessResult(xData, status) {
$(xData.responseXML).find("z\\:row").each(function () {
$("#data").append("<li>" + $(this).attr("ows_Title") + "</li>");
});
}
</script>
</head>
<body>
<div>
<ul id="data"></ul>
</div>
</body>
</html>
Comments
Post a Comment