Tuesday, August 16, 2011

Calling List webservice using Jquery

Most of the times we generally use object model to query SharePoint List.But not all development environments gives you an option of custom code.So JQuery along with Content Editor WebPart.
Here is the scenario I had a case of using dataview webpart which needs to be filtered basing on criteria, as we can create a parameter based on query string.In this case I am intrested in the filter rather than how to create DVWP.So coming to the coding part
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/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>AppStatusList</listName> \
<viewfields> \
<viewfields> \
<fieldref Name='Title' /> \
</ViewFields> \
</viewFields> \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>";

$.ajax({
url: "http://balaapd004/dept/cs/test/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset=\"utf-8\""
});

$('#ddlSelect').change(function() {
//var selectedValue=
$('#txtResult').val($('#ddlSelect option:selected').text());
});
});

function processResult(xData, status) {
var listItems="";

$(xData.responseXML).find("z\\:row").each(function() {
listItems+= "<option value='" + $(this).attr("ows_ID") + "'>" + $(this).attr("ows_Title") + "</option>";
$("#ddlSelect").html(listItems);

});
}
</script>

<select id="ddlSelect">
</select>
<input type="text" id="txtResult" />

No comments:

Post a Comment