Can GetListItems be filtered by item ID?

Jan 17, 2014 at 6:23 PM
I want to display only one item of the GetListItems result set based on the link a user clicks on.

I'm thinking of grabbing the query string ID from the href (for example, ".../Lists/Entities/DispForm.aspx?ID=358") and using that to filter the result so that when the user clicks on an item link, only that item's details are displayed.
Coordinator
Jan 17, 2014 at 6:46 PM
Sure. Just pass the CAML you need in the CAMLQuery.

M.
Jan 17, 2014 at 7:22 PM
Edited Jan 17, 2014 at 7:22 PM
Thanks, Marc.

What's the syntax? I'm new to CAMLQuery.

I looked at the documentation. It doesn't list CAMLQuery. Is this it?
<Query>
   <Where>
      <Lt>
         <FieldRef Name="ID" />
         <Value Type="Counter">3</Value>
      </Lt>
   </Where>
</Query>
Jan 21, 2014 at 1:01 PM
Hi Marc,

I added the CAMLQuery, but now I'm getting no results:
    var htmlTable = '<table>';
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Attributes",
    CAMLQuery: "<Eq><FieldRef Name='ID'></FieldRef><Value Type='Integer'>758</Value></Eq>",
    CAMLViewFields: "<ViewFields>" +
                "<FieldRef Name='Title' ></FieldRef>" +
                "<FieldRef Name='Values' ></FieldRef>" +
                "<FieldRef Name='ID' ></FieldRef>" +
            "</ViewFields>",
    
    completefunc: function (xData, Status) {
        $(xData.responseXML).SPFilterNode('z:row').each(function() {
            
            htmlTable += "<tr>" +
                "<td>" + $(this).attr("ows_Title") + "</td>" +
                "<td>" + $(this).attr("ows_Values") + "</td>" +
                "<td>" + $(this).attr("ows_ID") + "</td>" +
            "</tr>";
        })
        htmlTable += "</table>";
    }
});
Coordinator
Jan 21, 2014 at 1:35 PM
Are you getting any errors? Are you sure you have an item with ID=758?

M.
Jan 21, 2014 at 1:46 PM
I'm not getting any errors. Yes, I have 758. I've tried it with many other list item ID's..same result.
Coordinator
Jan 21, 2014 at 1:49 PM
If you remove the CAMLQuery, do you get results?

M.
Jan 21, 2014 at 1:51 PM
Yes, if I remove the CAMLQuery, the operation works fine.
Jan 21, 2014 at 6:10 PM
Edited Jan 21, 2014 at 6:10 PM
myahia wrote:
Thanks, Marc.

What's the syntax? I'm new to CAMLQuery.

I looked at the documentation. It doesn't list CAMLQuery. Is this it?
<Query>
   <Where>
      <Lt>
         <FieldRef Name="ID" />
         <Value Type="Counter">3</Value>
      </Lt>
   </Where>
</Query>
myahia wrote:
   var htmlTable = '<table>';
$().SPServices({
   operation: "GetListItems",
   async: false,
   listName: "Attributes",
   CAMLQuery: "<Eq><FieldRef Name='ID'></FieldRef><Value Type='Integer'>758</Value></Eq>",
   CAMLViewFields: "<ViewFields>" +
               "<FieldRef Name='Title' ></FieldRef>" +
               "<FieldRef Name='Values' ></FieldRef>" +
               "<FieldRef Name='ID' ></FieldRef>" +
           "</ViewFields>",
   
   completefunc: function (xData, Status) {
       $(xData.responseXML).SPFilterNode('z:row').each(function() {
           
           htmlTable += "<tr>" +
               "<td>" + $(this).attr("ows_Title") + "</td>" +
               "<td>" + $(this).attr("ows_Values") + "</td>" +
               "<td>" + $(this).attr("ows_ID") + "</td>" +
           "</tr>";
       })
       htmlTable += "</table>";
   }
});
Your CAMLQuery is different than the original one you posted. Wrap in with <Query> and <Where>. I'd also like to see you change the value type to Counter, but Integer does seem to work FWIW.

Cheers,
Matthew
Jan 21, 2014 at 6:36 PM
Works great now! Thanks a million!!
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>758</Value></Eq></Where></Query>",