Retrieve a column value from a list using the item ID

Sep 7, 2012 at 5:27 PM
Edited Sep 7, 2012 at 5:29 PM

I am attempting to retrieve a column value from a list for a specific list item using the item's ID.

I've included the code I'm attempting to use below. It's not working as is.

Thoughts?

function getCategory(intID) {
 var strCategory = '';
 
 $().SPServices({
  operation: "GetListItems",
  async: false,
  listName: "Ideas",
  ID: intID,
  CAMLViewFields: "<ViewFields Properties='True' />",
  completefunc: function (xData, Status) {
    strCategory = $(this).attr('ows_Category');
   }
 });
 
 return strCategory;
}

Sep 7, 2012 at 5:39 PM

OBTW - I've written it this way:

function getCategory(intID) {
 var strCategory = '';
 
 $().SPServices({
  operation: "GetListItems",
  async: false,
  listName: "Ideas",
  CAMLViewFields: "<ViewFields Properties='True' />",
  completefunc: function (xData, Status) {
   $(xData.responseXML).SPFilterNode("z:row").each(function() {
    if(intID == $(this).attr('ows_ID')) {
     strCategory = $(this).attr('ows_Category');
    }
   });
  }
 });
 
 return strCategory;
}

and it works.

I'm just not feeling all fuzzy about having to take a look at each and every item in the list. This list is going to get very large.

Coordinator
Sep 7, 2012 at 6:28 PM
Edited Sep 7, 2012 at 8:33 PM

You can filter in the CAMLQuery as well and only retrieve the one items you need, if desired. For instance:

$().SPServices({
  operation: "GetListItems",
  async: false,
  listName: "Ideas",
  CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + varID + "</Value></Eq></Where></Query>",
  CAMLViewFields: "<ViewFields Properties='True' />",
completefunc: function (xData, Status) { $(xData.responseXML).SPFilterNode("z:row").each(function() { strCategory = $(this).attr('ows_Category'); }); } });

M.
 

Sep 7, 2012 at 7:57 PM

Do we have to include this line?

$(xData.responseXML).SPFilterNode("z:row").each(function() {

}

I know it will loop only once, but it might be more effecient for a very busy site to not loop at all.

Coordinator
Sep 7, 2012 at 8:55 PM

Not really. I left it in place because I answered fast. ;+) The nice thing about the loop is that it can help you avoid some error handling. Also, since SharePoint uses the z:row namespace, which causes issues in some nonIE browsers, you can use my SPFilterNode function to ensure that the selector always works.

M.

Sep 7, 2012 at 9:16 PM

Got it. You know how we function, always trying to squeeze as much as possible out of as little code as possible. :)

Thanks for the help and thanks for the speed in which you helped :)

Coordinator
Sep 7, 2012 at 9:57 PM

No problem!

M.