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

  $().SPServices(params);

  return user;
}

Similar Posts

27 Comments

  1. Hi Marc,

    How can I output the value of returned user profile to controls or variables? I meant that the function returns a user profile. I would like to show the values in the user profile properties on a text box for example. How can I do this?

    1. June:

      Keep in mind that “controls” on the client side are just markup in the DOM. Using jQuery, you can parse apart the returned XML and put those values into the DOM anywhere you’d like.

      M.

  2. Thank you for the post.. but I would like to know, can I get the city, address, country, state, postal code, etc other information about current login user??

      1. Hi Marc, where is the example, could you please kindly show us the example, how to call the above method? what input to feed and how to use the output object.
        thanks in advance -Raj.

      2. //passing my people picker selected name to below method upon people picker select change event, value passed to method is correct

        var UserDetails=get_user_profile_by_login(ToName.replace(“span”,””));
        alert(objToString(UserDetails));

        ///return object is always null or undefined

        please help to suggest what is gg wrong here

  3. Hi, This code is not working everytime(out of 10 times works 1 time) in IE8, could you please help me out on this.

  4. Dear Marc
    How to get the current user id for the method. I need the id to update the list having user (person column)

  5. Hi Marc, would I be able to get the “Company” of the user via this method. “Company” is actually listed under “Custom Properties” when I click on the user name in SharePoint. Thanks,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.