Reputation: 415
I'm querying a SPList
and I'm retrieving a Person or Group
field object.
I want to get the DisplayName
and Email
properties but I don't know how to get them. Getters "get_email()
" and "get_displayName()
" throw error in console: "Unsupported object property or method"
I just have the Person or Group
object by storing it in a variable directly from a SPList
:
var person = found.get_item( "Assigned
" );
How to get person.get_displayName()
?
Upvotes: 0
Views: 13214
Reputation: 4208
We can use fieldValue.get_lookupValue() to get the user's display name. To get more information of the user we can use the web.ensureUser() method.
The code below for your reference:
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getItemFromList, "sp.js");
function getItemFromList(){
var clientContext = new SP.ClientContext();
var item = clientContext.get_web().get_lists().getByTitle("MyTasks").getItemById(1);
clientContext.load(item);
clientContext.executeQueryAsync(
function(){
// successfully retrieved value from list item
var assigned = item.get_item("AssignedTo");
if(assigned.length>0){
var user = clientContext.get_web().ensureUser(assigned[0].get_lookupValue());
clientContext.load(user);
clientContext.executeQueryAsync(
function(){
// successfully ensured user from user name
var email = user.get_email();
var login = user.get_loginName();
var displayName = user.get_title();
alert("User LoginName: " + login + "\nUser Email: " + email + "\nUser Display Name: " + displayName);
},function(sender,args){ // on error
alert(args.get_message());
}
);
}
},
function(sender,args){ // on error
alert(args.get_message());
}
);
}
</script>
In SharePoint 2013, I suggest you use REST API with jQuery Ajax to achieve it. Check the article below:
SharePoint 2013: Get User Details from Person or Group field using REST API
Upvotes: 4