Help with GetListItems and lookup

Apr 16, 2010 at 2:19 PM

Hi, i need to retrieve datas from a custom list that contains the Title field and a lookup field: no problem to get Title field but i'm unable to retrieve datas from the lookup field.

How can i retrieve and read data from the lookup field?

At the moment I use (Target_x0020_Audiences is the lookup field):

$().SPServices({
operation: "GetListItems",
listName: '_Tables',
CAMLViewFields:'<ViewFields><FieldRef Name="Title"/><FieldRef Name="Target_x0020_Audiences"/></ViewFields>',            
async: false,
completefunc: function (xData, Status) {
$(xData.responseXML).find("z\\:row");.each(function() {
...

});

});

Coordinator
Apr 16, 2010 at 3:07 PM
Edited Apr 16, 2010 at 3:07 PM

laplace82:

Hard to say without seeing what the ... is doing in your code.  However, I've dummied something up that might get you further along below. Note that I've changed the z:row selector.  See this blog post for the reason for this.

$().SPServices({
  operation: "GetListItems",
  listName: '_Tables',
  CAMLViewFields:'<ViewFields><FieldRef Name="Title"/><FieldRef Name="Target_x0020_Audiences"/></ViewFields>',            
  async: false,
  completefunc: function (xData, Status) {
    $(xData.responseXML).find("[nodename=z:row]");.each(function() {
      var itemTitle = $(this).attr("ows_Title");
      var itemAudience = $(this).attr("ows_Target_x0020_Audiences");
      ...
    });
  });
});

M.

Apr 16, 2010 at 3:38 PM

The list pointed by the lookup has 2 columns: Title and Groups, a "Person and Group" field that can be multi-value.

Whit the same syntax used by you too i can retrieve the text contained in the field Title of the list _Tables, but i'm not able to retrieve the value/values contained in the Groups column (the lookup).

var itemAudience = $(this).attr("ows_Target_x0020_Audiences") return a vector of 23 elements (the lookup has only 2 columns!) but i don't know ho to access to their values: if i write for(i = 0; i < itemAudience.length; i++)
      alert(itemAudience[i]) i only get undefined values.

$().SPServices({
operation: "GetListItems",
listName: '_Tables',
CAMLViewFields:'<ViewFields><FieldRef Name="Title"/><FieldRef Name="Target_x0020_Audiences""/></ViewFields>',            
async: false,
completefunc: function (xData, Status) {
var v = $(xData.responseXML).find("z\\:row").each(function() {
      var itemTitle = $(this).attr("ows_Title");
      var itemAudience = $(this).attr("ows_Target_x0020_Audiences");
      alert(itemTitle);alert(itemAudience .length);
      for(i = 0; i < itemAudience.length; i++)
      alert(itemAudience[i])
});
}
});

 

Coordinator
Apr 16, 2010 at 4:34 PM
Edited Apr 16, 2010 at 5:21 PM

In my test environment, what I see in an Audiences column is semi-colon separated values. (Sharepoint often uses this convention for multi-select columns.)  You're trying to treat that single text string as if it is an array, which it isn't.  You'll need to use the split function to convert the values to an array.

Something like this:

  var audienceValueArray = itemAudience.split(";");

M.