0

I want to get a User via JSOM.

The User is not the current user. The object I want to get should be SP.User (not String or anything) I want to get the User by asking for his login ("domain\username") and get back the SP.User object.

I found some solutions using the PeopleManager. But I'm not sure if this is the correct way. Because if I get the PreferredName, isn't this just a string?

Thanks

1 Answer 1

3

SP.Web.siteUsers property gets the collection of all users that belong to the site collection:

var value = SP.Web.get_siteUsers ()

How to get User client Object by email

function getUser(userName,Success,Error)
{
   var context = new SP.ClientContext.get_current();
   var web = context.get_web();
   var user = web.get_siteUsers().getByEmail(userName);


   context.load(user);
   context.executeQueryAsync(function(){
       Success(user);  
     },
     Error
   );
}




//Usage
getUser('[email protected]',function(user){
       console.log('User Id: ' + user.ID);
    },
    function(sender,args){
       console.log(args.get_message());
    }
);

Note: in order to retrieve User by LoginName replace the method getByEmail with getByLoginName:

var user = web.get_siteUsers().getByLoginName(userName);
2
  • Great, I'm glad it helped. Feb 18, 2014 at 15:01
  • What was the format of userName when using getByLoginName? Was it something like i:0#.w|domain\john.smith? getByLoginName doesn't seem to work for me using i:0#.w|domain\john.smith, domain\john.smith or john.smith
    – A G
    Mar 7, 2018 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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