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; }
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?
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.
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??
Vikram:
If the content is in the user profile store and you have the appropriate permissions, then the answer is yes.
M.
Can you show an example? This isn’t working at all.
There’s an example in the post.
M.
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.
HI Marc, i forgot to mention that i am using Sharepoint Foundation 2010, hope there is no dependency with this version.
@Raj:
If you’re on SharePoint 2010 Foundation, you don’t have the User Profile Service, as indicated in the docs.
M.
//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
Hi, This code is not working everytime(out of 10 times works 1 time) in IE8, could you please help me out on this.
It’s hard to say why it wouldn’t be working without more details. Best to post your code and the details to the Codeplex Discussions.
M.
how to get the current user image using spservices sir…
@yog:
You’d need to look for the PictureURL property.
M.
Dear Marc
How to get the current user id for the method. I need the id to update the list having user (person column)
@Achyut:
The current user’s id is available in a JavaScript variable in the page. See http://sympmarc.com/2013/03/26/using-_sppagecontextinfo-to-determine-the-current-sharepoint-context-in-script/ for a [hopefully] complete list.
M.
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,
Hi,
Please help, is posible show this information in anonymous acces?
thanks in advance.
@Jorge:
An anonymous user doesn’t have a user profile, so there’s nothing to retrieve.
M.
in sharepoint 2007, sorry