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

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('username@tenant.onmicrosoft.com',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);
  • Great, I'm glad it helped. – Vadim Gremyachev Feb 18 '14 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 '18 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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