SP.UserProfiles - get all properties available for user
-
07-10-2020 - |
Frage
Since I do not have access to the SharePoint farm.
Is there any way to retrieve all available property names from UserProfiles in SharePoint 2013 through JavaScript?
What I was looking for is the names you pass as an array with the following call:
var object = new SP.UserProfiles.UserProfilePropertiesForUser(context, accountName, propertyNames);
Lösung
From MSDN
Retrieve user profile properties by using the JavaScript object model in SharePoint 2013
var personProperties;
// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
function getUserProperties() {
// Replace the placeholder value with the target user's credentials.
var targetUser = "domainName\\userName";
// Get the current client context and PeopleManager instance.
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
// Get user properties for the target user.
// To get the PersonProperties object for the current user, use the
// getMyProperties method.
personProperties = peopleManager.getPropertiesFor(targetUser);
// Load the PersonProperties object and send the request.
clientContext.load(personProperties);
clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}
// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
// Get a property directly from the PersonProperties object.
var messageText = " \"DisplayName\" property is "
+ personProperties.get_displayName();
// Get a property from the UserProfileProperties property.
messageText += "<br />\"Department\" property is "
+ personProperties.get_userProfileProperties()['Department'];
$get("results").innerHTML = messageText;
}
// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
$get("results").innerHTML = "Error: " + args.get_message();
}
Using REST API
/_api/SP.UserProfiles.PeopleManager/GetMyProperties
Need to make a GET request to the above end point. It will return current user's properties.
Andere Tipps
To retrieve all the properties, you can use REST api such as:
var url = _spPageContextInfo.webServerRelativeUrl + "/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='" + encodeURIComponent(targetUsername) + "'&$select=UserProfileProperties";
then you can look up all the available properties within 'properties' variable using FireBug/IE dev tool console.
var properties = data.d.UserProfileProperties.results;
window.console && console.log(properties);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange