SPServices Example: UserProfileService.GetUserProfileByName

People post working bits of code all the time on the SPServices site at Codeplex, and I don’t always do a good job of making them known to others. Usually I add good examples to the documentation pages, but I don’t always get to it. Sometimes by seeing what someone else has done, we get inklings of what we may be able to do ourselves. Of course, there’s also the willy-nilly copy-and-paste from the Interwebs approach, but I discourage using others’ code (including mine) without understanding what it does.

All that said, nileshc posted a nice little snippet the other day that I thought I’d share. SPServices has a function called SPGetCurrentUser that returns, well, information about the current user. I wrote it early on based on a trick from Einar Otto Stangvik (@einaros). It’s a good function because it works with any version or license of SharePoint (2007 and 2010), but it’s not exactly efficient or elegant. It basically loads the _layouts/userdisp.aspx page and screen scrapes the values from it. (Who knew that screen scraping would once again be cool, and even useful?)

If you’re using MOSS or SharePoint Server 2010, then you also have the User Profile Service at your disposal. nileshc‘s example takes advantage of this Web Service to grab information about the current user from the User Profile using GetUserProfileByName instead, which gives you a lot more to work with. I’ve adapted nileshc’s script slightly to make the call to SPGetCurrentUser if the logon isn’t currently known.

function get_user_profile_by_login(login) {

  var user = {};

  var params = {
    operation: 'GetUserProfileByName',
    async: false,
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("PropertyData").each(function() {
        user[$(this).find("Name").text()] = $(this).find("Value").text();
      }); // end each

      // Easy names
      user.login = user.AccountName;
      user.full_name = user.PreferredName;
      user.email = user.WorkEmail;

    } // end completefunc

  if (login != null) {
    params.accountName = login;
  } else {
    params.accountName = $().SPServices.SPGetCurrentUser({
      fieldName: "Name"


  return user;

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