Capturing the Current User’s Manager in MOSS Using SPServices and GetUserProfileByName

I had a question today about how to get the current user’s manager and put the value into a Person or Group column in a list. This one only works in MOSS or SharePoint Server 2010 (not WSS 3.0 or SharePoint Foundation) because the UserProfileService Web Service is only available there.

The code is relatively straightforward: a call to SPServices to get the results from  GetUserProfileByName and then a little more code to poke it into the appropriate Person or Group column. In this case, the Person or Group column is called ‘Manager’.

$(document).ready(function() {

  var managerName;
  $().SPServices({
    operation: "GetUserProfileByName",
    async: false,
    AccountName: $().SPServices.SPGetCurrentUser(),
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("PropertyData > Name:contains('Manager')").each(function() {
        managerName = $(this).parent().find("Values").text();
      });
    }
  });

  // There's no easy way to find one of these columns; we'll look for the comment with the columnName
  var searchText = RegExp("FieldName=\"Manager\"", "gi");
  // Loop through all of the ms-formbody table cells
  $("td.ms-formbody").each(function() {
    // Check for the right comment
    if(searchText.test($(this).html())) {
      $(this).find("div[Title='People Picker']").html(managerName);
      return false;
    }
  });
}); // End $(document).ready

In line 8, you can change the value Manager to anything else you’d like to get back from the GetUserProfileByName operation, and in line 14, you could change the Fieldname to whatever your column name for the Manager might be.

The available values from GetUserProfileByName in my test MOSS environment are:

  • UserProfile_GUID
  • AccountName
  • FirstName
  • LastName
  • PreferredName
  • WorkPhone
  • Office
  • Department
  • Title
  • Manager
  • AboutMe
  • PersonalSpace
  • PictureURL
  • UserName
  • QuickLinks
  • WebSite
  • PublicSiteRedirect
  • SPS-Dotted-line
  • SPS-Peers
  • SPS-Responsibility
  • SPS-Skills
  • SPS-PastProjects
  • SPS-Interests
  • SPS-School
  • SPS-SipAddress
  • SPS-Birthday
  • SPS-MySiteUpgrade
  • SPS-DontSuggestList
  • SPS-ProxyAddresses
  • SPS-HireDate
  • SPS-LastColleagueAdded
  • SPS-OWAUrl
  • SPS-ResourceAccountName
  • SPS-MasterAccountName
  • Assistant
  • WorkEmail
  • CellPhone
  • Fax
  • HomePhone

40 Comments

  1. Thanks Marc, another great contribution to the community!

    NOTE: I had to change Line 08 to the following to get it to work. (had to use :contains() instead of [text=]

    $(xData.responseXML).find(“PropertyData Name:contains(‘Manager’)”).each(function() {

    Reply
  2. This did me well today Marc, thank you! For attempting to get other properties, go into the ‘Manage User Properties’ page in your SSP (2007) or Service Application (2010), and Edit the property you need the internal name of. The Name: field at the top of the page (should be grayed out), is what the property name you need to grab is.

    Reply
    • George:

      I don’t think that there’s any one step way to do it. You could probably get something to work using a combination of the Users and Groups Web Service and the UserProfileService. The easiest situation would be where you have a SharePoint group containing those users. Then you could use GetUserCollectionFromGroup.

      M.

      Reply
  3. Marc, is there a way to find the same information when looking up a user from a list in a people/group field in replacement for $().SPServices.SPGetCurrentUser()?

    Thanks for the post, Erich

    Reply
    • Erich:

      I’m not sure what you mean. Can you explain a little more? If you want to get infomation from columns in a list, you can use GetListItems, but I’m not sure if that’s your goal.

      M.

      Reply
      • Sorry for the confusion. If one of the items your returning from a list is a user, is there a way to look up that user’s manager?

        Thanks again. Erich

        Reply
        • Erich:

          Well, sure. You can take the user’s account and pass that instead of $().SPServices.SPGetCurrentUser() for the AccountName. In the example, I’m just using $().SPServices.SPGetCurrentUser() as a quick way to get the current user’s account. So, you could have:

            $().SPServices({
              operation: "GetUserProfileByName",
              async: false,
              AccountName: myUserVar,
              completefunc: function (xData, Status) {
               $(xData.responseXML).find("PropertyData").find("Name[text=Manager]").each(function() {
                managerName = ($(this).parent().find("Values").text());
               });
              }
          

          where myUserVar = “domain\\username”;

          M.

          Reply
          • thanks, one other question. How do you get “domain\username” info if one of the items in a list is a user, when I print it out to the page it comes back in the following format: “11;#Smith, Joe” not “domain\username”

            Reply
            • Try using GetUserProfileByIndex instead and pass:
              index: 11
              I’m not positive this will work, but it seems like the right answer.

              If it doesn’t, then you’ll need to query the User Information List with GetListItems to get the account.

              M.

              Reply
    • Matthias:

      SPServices takes care of setting the URL for you. By default it will use the current context, meaning that it will use “http://site/_vti_bin/userprofileservice.asmx” just like in that post, where “site” is the path to the site where your user is currently. It might help to post your code in the SPServices discussions.

      M.

      M.

      Reply
  4. I’d like to respond to a change in the person or group coulmn and change other columns accordingly. So, if a user other than the current user is subsequently selected, I’d like to key off of that and re-populate other columns based on user profile fields (telephone number, etc.).

    Can you provide any guidance? The only way I have come up with so far is to replace a function in core.js which I know is a bad idea.

    How can I respond to changes in the people picker?

    Reply
    • Scott:

      You can use jQuery and SPServices for this. You certainly shouldn’t change core.js! EVER!

      If you take a look at the first page of the SPServices documentation (down at the bottom), you can see how you might add script to the page. I also have multiple blog posts, as do people like Mark Rackley and others, which explain how best to use script with SharePoint.

      M.

      Reply

Have a thought or opinion?