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
Excellent write up. Thanks for posting all of the possible values you can find within the method: GetUserProfileByName
Definitely a time saver!
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() {
Matt:
You’re welcome! I’m curious about the change you had to make. Is the User Property in your environment not called “Manager”?
M.
It is exactly “Manager” — very strange indeed.
That *is* weird. Can you email me example XML from your environment? Confidential, of course.
M.
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.
Hi Marc:
Is there a way to use SPServices to return a list of _all_ users, say, where Department=3200?
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.
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
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.
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
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:
where myUserVar = “domain\\username”;
M.
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”
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.
Hi Marc,
tried your solution, but always get an error “The user profile manager object could not be loaded…”.
Is there a way to set the profileservice.Url like mentioned here:
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/29793892-9d31-45a5-9455-63021954d74b/
Thanks for your help
Matthias
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.
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?
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.
I tried your code in newform.aspx but i got object expected at $(document).ready(function() {
VR:
That error indicates that you haven’t successfully referenced the jQuery library.
M.
Yes I read your first post and add referenced jQuery library. Now i successfully read manager of current user and display in people picker control.
Thanks.
VR
Can not get the value of a field “Manager”. Get the message “Undefined”. Although all the fields in the user profile filled.
Vitaly:
You should inspect the returned XML to ensure that the Manager column is indeed there, and thaat it is populated as you expect.
M.