javascript - Retrieve displayName and email from person or group list field in SharePoint - Stack Overflow

I'm querying a SPList and I'm retrieving a Person or Group field object.I want to get the Dis

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() ?

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() ?

Share Improve this question asked Mar 21, 2018 at 16:21 Fuczi FucziFuczi Fuczi 4152 gold badges5 silver badges13 bronze badges 1
  • check this, you cant directly get the user details, you will have to get using the userid sharepoint.stackexchange./questions/176319/… – Guruparan Giritharan Commented Mar 22, 2018 at 7:14
Add a ment  | 

1 Answer 1

Reset to default 4

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744209259a4563255.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信