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,

    I need to fetch login-name by using email entered in a text-box. is it possible using spservices.

  2. Hi Marc, I am using SPServices getuserprofilebyname and it’s great…one question though: when I make my query managed metadata columns with multiple terms (e.g. SPS-Interests) return wiht all the terms bunched together. I would have expected them to have some sort of delimiter like ;# , but nope, they don’t. E.g. for a user who has added SCUBA Diving, Football and Technology to their “Interests” profile field, the SPServices getuserprofilebyname query returns as

    SCUBA DivingFootballTechnology.

    Any suggestions on how to return these so I can get at them? Thanks.

    1. @Paul:

      That sounds like it may be a bug in the way I’m formatting the output. Yours is the first report in all these years. What version of SPServices are you using, and what version of SharePoint?

      M.

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.