views:

3038

answers:

2

The code below seemingly executes the web service and returns values, but ignores the where clause (thus returning all items in the list). This is the simplest form of the problem that I've come up with.

The TestQuery list is a simple custom list with no user defined fields. Can anyone see why the filter is not working?

<body>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
$(document).ready(function() {
    var soapEnv = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'&gt;&lt;soapenv:Body&gt;&lt;GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'&gt;";
    soapEnv += "<listName>TestQuery</listName>";
    soapEnv += "<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>One</Value></Eq></Where></Query>";
    soapEnv += "<ViewFields><ViewFields><FieldRef Name='Title'/></ViewFields></ViewFields><RowLimit>1</RowLimit>";
    soapEnv += "</GetListItems></soapenv:Body></soapenv:Envelope>";

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

function processResult(xData, status) {
         $('#WSResponse').text(status);
    $(xData.responseXML).find("z\\:row").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
    });
    //}
}
</script>


<ul id="tasksUL"/>
<div id="WSResponse"/>

</body>
+1  A: 

You're missing a <query>. The formatting of those parameters is somewhat counterintuitive.

There's a post on my blog with a working example:

http://www.tqcblog.com/archive/2007/09/24/sharepoint-blog-content-rating-with-javascript-and-web-services.aspx

Tom Clarkson
Thanks ever so much - I've been trying various options all day!
stuckagain
+1  A: 

I think you need to put the Query tag inside a query tag and the ViewField inside a viewField tag so something like:

var soapEnv = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'&gt;&lt;soapenv:Body&gt;&lt;GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'&gt;"; 
    soapEnv += "<listName>TestQuery</listName>"; 
    soapEnv += "<query><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>One</Value></Eq></Where></Query></query>"; 
    soapEnv += "<viewFields><ViewFields><FieldRef Name='Title'/></ViewFields></viewFields><RowLimit>1</RowLimit>"; 
    soapEnv += "</GetListItems></soapenv:Body></soapenv:Envelope>";
Temple