Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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"));

            });

        }
    });
share|improve this question
add comment

1 Answer 1

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);
share|improve this answer
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. Mar 12 at 13:11
    
Paul, thanks for your comment, the answer has been updated. –  Vadim Gremyachev Mar 12 at 15:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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