Operation GetListItems is not working when I insert the CAML query to filter the options.

Nov 16, 2012 at 4:44 PM
Edited Nov 16, 2012 at 4:45 PM

Hello CodePlex Users,

I am trying to filter the items from a list based on the current user; I started playing around with this script:

<script language="javascript" type="text/javascript"> $(document).ready(function() { $().SPServices({ operation: "GetListItems", async: false, listName: "Sites", CAMLQuery: "<Where><Eq><FieldRef Name='UserName'/><Value Type='Integer'><UserID Type='Integer'/></Value></Eq></Where>", CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='RecursiveAll' IncludeRootFolder='True' /></QueryOptions>", CAMLViewFields: "<ViewFields><FieldRef Name='SiteName' /><FieldRef Name='UserName' /><FieldRef Name='Title' /></ViewFields>", completefunc: function (xData, Status) { $(xData.responseXML).SPFilterNode("z:row").each(function() { var liHtml = " + $(this).attr("ows_SiteName") + ""; $("#tasksUL").append(liHtml); }); } }); }); </script>
<ul id="tasksUL"/>


However it doesn’t bring the items up, when I eliminate the Query and Query options, it does extract every item, my guess is that my query is not correct, can someone more experienced give me a hand? It will be highly appreciated.

Nov 17, 2012 at 3:34 AM


The problem is that the value you are specifiying for the UserName isn't defined. You'll need to have a value. You may be able to use SPGetCurrentUser to obtain it, but it's not totally clear what your UserName column contains. Assuming it's an ID, you should be able to get it to work with something like this for the current user, anyway:

     CAMLQuery: "<Where><Eq><FieldRef Name='UserName'/><Value Type='Integer'>" + $().SPServices.SPGetCurrentUser({fieldName: "ID"}) + "</Value></Eq></Where>",


Nov 19, 2012 at 2:54 PM

Hello M,

I did change the query as you suggest, but no records are retreived. I even created a new list to test it out, I have two columns, the Title (contains department names) and the Manager, where manager is a people picker field set up to display the ID. Here is the modified code:

$(document).ready(function() {
     operation: "GetListItems",  
     async: false,  
     listName: "Departments",
     CAMLQuery: "<Where><Eq><FieldRef Name='Manager'/><Value Type='Integer'>" + $().SPServices.SPGetCurrentUser({fieldName: "ID"}) + "</Value></Eq></Where>",  
     CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='RecursiveAll' IncludeRootFolder='True' /></QueryOptions>",  
     CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Manager'/></ViewFields>",  
     completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "
  • " + $(this).attr("ows_Title") + ""; $("#tasksUL").append(liHtml); }); } }); }); </script>
  • I am using jquery-1.8.2.min and SPServices-0.7.2.min
    Nov 19, 2012 at 3:53 PM

    As I said, what I suggested was only going to be *like* what you'll end up with.

    I'd suggest querying the list without filters to see what the data in the Manager column looks like. You'll need to mimic that format in the CAML you're using to filter.