2

Using the below SPServices call, I can get the display name of the author of a list item:

$().SPServices({
                operation: "GetListItems",
                async: false,
                listName: "MyList",
                CAMLViewFields: "<ViewFields Properties='True' />",
                CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Integer'>" + id + "</Value></Eq></Where></Query>", 
                completefunc: function (xData, Status) {
                    $(xData.responseXML).find("[nodeName='z:row']").each(function() {
                        author = $(this).attr("ows_Author");
                    });
                }
            });

This gets me a name like "43#;John Doe". How can I get the account name instead? Like "john.doe". So I can use it to get the user's manager's account name.

Thanks.

4 Answers 4

3

You need to use the Users and Groups web service. If you call the GetUserCollectionFromWeb() method, it will return an XML node containing all the users on the site. You can then look up your specific user using the ID returned in your User node - in the case of the example - 43.

The web service will return a collection of User nodes which have an ID attribute one of which will match your ID, and a LoginName attribute which will be the information you are after.

The SPService reference to the service is here

The MSDN reference to the service is here

2

I've read that if you include:

<QueryOptions><ExpandUserField>TRUE</ExpandUserField></QueryOptions>

in your CAMLQuery, it will bring back the account name in addition to the display name.

However, it is not working in WSS for me, and I vaguely recall it might only work in MOSS.

1

I've done this same thing in C#, using the format style of the username you provided, and used the class SPFieldUserValue to get the actual user object, seen below:

string user = new SPFieldUserValue(web, "43#;John Doe").User.Name;

I'm not familiar enough with the client object model to provide an example for you, but take a look here. See if either of the two solutions provided help you. They are using the FieldUserValue class, the client object model implementation of the SPFieldUserClass.

0
0

Just modify it. If you can get: 43#;John Doe, then just do:

function getUserName(author) {
    var authorName = author.split(';')[1]; // 0 is "43#", while 1 is everything after the ";"
    var authorParts = authorName.split[' ']; // break it into an array based on spaces

    var authorUserName = "";

    // Now reconstruct it with dots
    for (var i=0; i<authorParts.length; i++) {
        authorUserName += authorParts[i] + ".";
    }
    authorUserName = authorUserName.trimEnd('.');
    return authorUserName;
}

And call it:

author = getUserName(author);

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.