Retrieve Lookup Columns and People Picker Columns

May 22, 2011 at 6:31 AM

I need to retrieve a specific list item and set a cookie to the result.

I am just starting to play with the GetLIstItems function.  Using the code below, I can query and display any text field without a problem.

BUT, when the field I want is actually a people picker field, I get "undefined" as the output.  BTW, this also happens for lookup columns.  What additional code must I add to be able to retrieve from the people picker column the current username in "domain/user" format?

 

Code:

<script src="/jquery-1.4.2.min.js" type="text/javascript"></script><script src="/jquery.SPServices-0.6.1.min.js" type="text/javascript"></script><script language="javascript" type="text/javascript">
$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    debug: true,
    listName: "LPR Users",
    CAMLViewFields: "<ViewFields><FieldRef Name='UserName' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName='z:row']").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
      });
    }
  });
});</script><ul id="tasksUL"></ul>

May 22, 2011 at 1:30 PM
Edited May 22, 2011 at 1:30 PM

To get any value back from the list, you have to make a reference to that particular column.  In your example above, you are referencing your column: UserName, but it's missing in one of the critical parts of the code as well.  To do anything with the data being returned from the server, you have to add a reference to xData.responseXML:

 

$(xData.responseXML).find("[nodeName='z:row']").each(function() {
	var userName = "<li>" + $(this).attr("ows_UserName") + "</li>";
	$("#tasksUL").append(userName);
});

To get domain\username from a People Picker column, you have to change a setting on the column.  When adding the column to the list, at the bottom, there is an option called: Show Field.  When changing this to: Account; the domain\username is stored instead of the normal Name (with presence) data.  Also note that since People Picker columns are lookups, the data retrieved will be in this format: 1;#Domain\UserName.  You have to run a little bit more code to get what you want.  Here's an example:

 

$(xData.responseXML).find("[nodeName='z:row']").each(function() {
	var userName = "<li>" + $(this).attr("ows_UserName").split(";#")[1] + "</li>";
	$("#tasksUL").append(userName);
});

 

Notice the difference?

May 22, 2011 at 6:10 PM

Sometimes you have to be slapped in the face with the obvious. 

 

Thanks for your response.  I was looking at the demand side of the function rather than the output side.