2

I am new to SPServices, I am trying to fetch list items from a list.

I am fetching the Title and a person field(Created By).

When i print the Created By field ,I get the person name say XYZ.

But i want to get his ID i.e DOMAIN\NO.

How can i achieve this? This is my code:

$().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "AppreciationList",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Appreciated_x0020_Person'/><FieldRef Name='Author'/></ViewFields>",
        completefunc: function (xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function () {


                alert($(this).attr("ows_Appreciated_x0020_Person")+ $(this).attr("ows_Title"));

            });

        }
    });

1 Answer 1

5

In order to return account name, email, and name instead of just the name, use:

CAMLQueryOptions: "<QueryOptions><ExpandUserField>True</ExpandUserField></QueryOptions>"

Refer SPServices GetListItems documentation for more details.

Example:

 $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "Tasks",
        CAMLQueryOptions: "<QueryOptions><ExpandUserField>True</ExpandUserField></QueryOptions>",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='AssignedTo'/></ViewFields>",
        completefunc: function (xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function () {
                console.log($(this).attr("ows_AssignedTo"));
            });
        }
    });

About ExpandUserField property

According to MSDN:

Gets or sets a Boolean value that specifies whether to use special rendering for user field values that makes them include the login name, e-mail address, Session Initiation Protocol (SIP) address, and title, when present, which causes a user field to behave as a multilookup field.

$().SPServices.SPXmlToJson

$().SPServices.SPXmlToJson function includes support for converting user properties into JSON, for more details follow this discussion.

Example: how to convert response into JSON for User field:

 var result = $(xData.responseXML).SPFilterNode("z:row").SPXmlToJson({
         mapping: { 
             ows_AssignedTo: { mappedName: "AssignedTo", objectType: "User"}  
         },
         includeAllAttrs: true
 });
 console.log(result[0].AssignedTo.userName);
2
  • 1
    Let me add: The return data from this expanded User field will include all the data above but it will be parsed as it is returned as a single string. I forget now what the delimeter is.
    – Paul T.
    Commented Mar 12, 2014 at 13:11
  • Paul, thanks for your comment, the answer has been updated. Commented Mar 12, 2014 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.