I want to get the user's manager using Javascript Client Object model.
How can I do this?
You create an instance of a manager object by using PeopleManager class in JSOM SharePoint 2013.
See the sample code below:
//Make sure SP.UserProfiles.js file is loaded before code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
var userProfileProperties;
function getUserProperties() {
// get the target users domain name and account name.
var targetUser = "SomeDomain\\SomeUserName";
// Get the current client context and PeopleManager instance.
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
// Specify the properties to retrieve
var profilePropertyNames = ["PreferredName", "Department"];
var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(
clientContext,
targetUser,
profilePropertyNames);
// Get user profile properties for the target user using the UserProfilePropertyFor method.
userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
// Load the UserProfilePropertiesForUser object and send the request.
clientContext.load(userProfilePropertiesForUser);
clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}
// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
var messageText = "\"PreferredName\" property is "
+ userProfileProperties[0];
messageText += "<br />\"Department\" property is "
+ userProfileProperties[1];
$get("results").innerHTML = messageText;
}
// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
$get("results").innerHTML = "Error: " + args.get_message();
}
In SharePoint 2010 and 2007, you can use SPServices library.
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();
alert(managerName );
});
}
});
The Above is the code used.
I want manger in alert. Version of the sharepoint Microsoft SharePoint Server 2010 not foundation.
@Falak Mahmood. Please help. I am getting this exception in google chrome
POST url/_vti_bin/UserProfileService.asmx 500 (Internal Server Error) jquery-1.8.2.min.js:2 send jquery-1.8.2.min.js:2 p.extend.ajax jquery-1.8.2.min.js:2 Q.fn.SPServices jquery.SPServices-0.7.1a.min.js:19 (anonymous function) made.js:25 k jquery-1.8.2.min.js:2 l.fireWith jquery-1.8.2.min.js:2 p.extend.ready jquery-1.8.2.min.js:2 D