사용자 이름을 ID로부터 가져 오십시오
-
10-12-2019 - |
문제
내 SharePoint Hosted App의 일환으로 사람이나 그룹이 포함 된 목록을 사용하고 있습니다.나는 다음을 사용하여 그 사람의 ID를 얻고 있습니다.
var ApproverID = currentItem.get_fieldValues()["Approver"].get_lookupId();
.
여기에서 이제는 사람 이름이나 "DisplayText"를 얻고 싶지만 MSDN 에서이 작업을 수행하는 클라이언트 측을 찾을 수 있습니다.
이 작업을 수행 할 수 있습니까?
해결책
I assume in your example Approver
is a User field type. In SharePoint 2013 CSOM SP.FieldUserValue object represents the value of a user field for a list item.
The following code demonstrates how to get User Name
from User field:
//Get user field value
var assignedToVal = item.get_item('AssignedTo');
var userName = assignedToVal.get_lookupValue();
console.log(userName);
//Get multi user field value
var assignedToVals = item.get_item('AssignedTo');
for(var i = 0; i < assignedToVals.length;i++) {
var userName = assignedToVals[i].get_lookupValue();
console.log(userName);
}
다른 팁
You need to reference following JavaScript file:
<SharePoint:ScriptLink ID="ScriptLink2" name="SP.UserProfiles.js" runat="server"
ondemand="false" localizable="false" loadafterui="true" />
Than you can access the User Profile Properties as follows:
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";
var targetUser = currentItem.get_fieldValues()["Approver"].get_lookupValue();
// 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();
}
Reference:
How to: Retrieve user profile properties by using the JavaScript object model in SharePoint 2013
제휴하지 않습니다 sharepoint.stackexchange