How do you get a User object from Person or Group field value using JSOM/JavaScript API? I am looking for something like this (See Vadim Gremyachev answer), but let me be clear I DO NOT WANT the current logged in user as in that code example:

user = clientContext.get_web().get_currentUser();

I want a User object that is populated by a person or group field from a list column! The overall goal is to have a user object so I can do things like:

alert('The Display Name of the user is ' + user.get_loginName());
alert('The Account Name of the user is ' + user.get_id());
alert('The email address of the user is ' + user.get_email());
alert('The User Title is ' + user.get_title());
etc.

Also note in my scenario I am only using single user people picker fields so no complicated arrays or user collections needed when getting the value from the people picker field.

My current environment is:
SharePoint 2013 Enterprise On-premise
share|improve this question

When you access a User field from a list item using the JavaScript Object Model, the value is treated as if it were a lookup field. You can only really invoke fieldValue.get_lookupValue() to get the user's display name and fieldValue.get_lookupId() to get the ID of the user's entry in the site collection user info list.

To get the actual SPUser object, so that you can drill down into the email and login name, you can use the SPWeb.ensureUser() method.

 var clientContext = new SP.ClientContext();
 var list = clientContext.get_web().get_lists().getByTitle("Example Code").getItemById(1);
 clientContext.load(item);
 clientContext.executeQueryAsync(
      function(){ // successfully retrieved value from list item
           var author = item.get_item("Author");
           var user = clientContext.get_web().ensureUser(author.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());
      }
 );
share|improve this answer

In this case I would use REST APIs to get the People/Group (Name with presence) column's username by expanding the column e.g. ....$expand=user/group

and then there is an ensureUser Method in the JS Client Object Model:

http://msdn.microsoft.com/en-us/library/ff408786.aspx

Which you could use to create User object.

share|improve this answer

You can use a code like this:

Below example uses JSOM

SP.SOD.executeFunc("sp.js", "SP.ClientContext", function(){
        SP.SOD.registerSod("sp.userprofiles.js", SP.Utilities.Utility.getLayoutsPageUrl("sp.userprofiles.js"));
        SP.SOD.executeFunc("sp.userprofiles.js", "SP.UserProfiles.PeopleManager", getTaskListItem);
});
        function getTaskListItem(){
        var item;    
            var clientContext = new SP.ClientContext.get_current();

            var web = clientContext.get_web();

            var list = web.get_lists().getByTitle('Tasks'); //replace with your custom list

            item = list.getItemById(1);

            clientContext.load(item);

            clientContext.executeQueryAsync(onSuccess, onFailure);
        }
        function onSuccess(){
        var context = SP.ClientContext.get_current();
        var web = clientContext.get_web();
        var userName = item.get_item("AssignedTo").get_lookupValue();//replace AssignedTo with your person column
        var user = web.ensureUser(userName);
        var email = user.get_email();
    var loginName = user.get_loginName();

        }
        function onFailure(){

            console.log('Failure!');
        }

If you are planning to use REST api, the call would be as below:

https://siteurl/_api/web/lists/getbytitle('Tasks')/items/?$select=ID,Title,Body,Issue/Id,TaskDueDate,Created,AssignedTo/FirstName,AssignedTo/LastName,AssignedTo/Name,AssignedTo/Id,AssignedTo/Email&$expand=Issue/Id,AssignedTo/Id

It can also be used as follows:

here AssignedTo is a person/group column, but you can add you custom person column as well.

var getTasks = function () {
    var promise = $.ajax({
        url: "/_api/web/lists/getbytitle('Tasks')/items/?" + 
            "$select=ID,Title,Body,Issue/Id,TaskDueDate,Created,AssignedTo/FirstName,AssignedTo/LastName,AssignedTo/Name,AssignedTo/Id,AssignedTo/Email&" +
            "$expand=Issue/Id,AssignedTo/Id",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
    });
    return promise;
};
share|improve this answer
    
Trying to use the JSOM code above and keep getting the below error; amI missing a script reference (see below.) – Shane Gibson Oct 24 at 15:09
    
The property or field 'Email' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. – Shane Gibson Oct 24 at 15:10
    
Here are the script references in the web part page: – Shane Gibson Oct 24 at 15:11
    
<SharePoint:ScriptLink name="clienttemplates.js" runat="server" LoadAfterUI="true" Localizable="false"/> <SharePoint:ScriptLink name="clientforms.js" runat="server" LoadAfterUI="true" Localizable="false"/> <SharePoint:ScriptLink name="clientpeoplepicker.js" runat="server" LoadAfterUI="true" Localizable="false"/> <SharePoint:ScriptLink name="autofill.js" runat="server" LoadAfterUI="true" Localizable="false"/> <SharePoint:ScriptLink name="sp.RequestExecutor.js" runat="server" LoadAfterUI="true" Localizable="false"/> – Shane Gibson Oct 24 at 15:13
    
<SharePoint:ScriptLink name="sp.js" runat="server" LoadAfterUI="true" Localizable="false"/> <SharePoint:ScriptLink name="sp.runtime.js" runat="server" LoadAfterUI="true" Localizable="false"/> <SharePoint:ScriptLink name="sp.core.js" runat="server" LoadAfterUI="true" Localizable="false"/> <SharePoint:ScriptLink name="SP.Debug.js" runat="server" LoadAfterUI="true" Localizable="false"/> <SharePoint:ScriptLink name="SP.Runtime.Debug.js" runat="server" LoadAfterUI="true" Localizable="false"/> – Shane Gibson Oct 24 at 15:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.