Skip to main content

Posts

Showing posts from October, 2019

Get User Id using REST or JavaScript Object Model

Sometimes you would need to fetch the User Id based on either Login name or Email id. You would need User Id if you need to assign a user object to a people picker control or People/Group field.

How do we get the Id based on Email or Login Name in client side development? We can achieve that using JavaScript Object Model or REST API. Let me share the first example using JavaScript Object Model (JSOM).

var context = new SP.ClientContext.get_current();
this.user = context.get_web().ensureUser(loginName or Email);

var o = { d: d, user: this.user };

context.load(this.user);
context.executeQueryAsync(
    Function.createDelegate(o, ensureUserSuccess),
    Function.createDelegate(o, Fail)
);

The above code fetches User Id for a given Login Name or Email Id.

Interestingly, there is no equivalent endpoint available in REST! The Microsoft documentation talk about a endpoint but I could not get it working.

So what is the way to get User Id using REST? You have to use the hidden User Information…