As the title suggests, does anyone have a working method for getting a users ID from a users Login Name (not the one signed in)?

Login name like: i:0#.f|membership|[email protected]

I am specifically working with extensions and can't find a way to achieve this.


1 Answer 1


You need to a make a POST call to the EnsureUser endpoint and pass the login name as the payload.

Try with the below sample code, modify the login name as per your requirement:

const payload: string = JSON.stringify({
  'logonName': this.context.pageContext.user.loginName // i:0#.f|membership|[email protected]      

var postData: ISPHttpClientOptions = {
  body: payload

var endPoint = `${this.context.pageContext.site.absoluteUrl}/_api/web/ensureuser`;

  .then((response: SPHttpClientResponse) => {
    return response.json();
  • Getting Error: Uncaught TypeError: Cannot read property 'web' of undefined. This is on ${this.context.pageContext.web.absoluteUrl. Any ideas?
    – Gaznae
    Commented Jul 6, 2018 at 17:42
  • Check if you are passing the correct site url in the rest endpoint. Try it first as hard coded url after that you can pass it to your component via props. Would be great if you can share some code. I am making a standard post request with payload. Commented Jul 6, 2018 at 18:10
  • I wrote this inside the render method of the webpart typescript (.ts) file. Commented Jul 7, 2018 at 4:31
  • 1
    Thanks Gautam, I changed the reference to ${this.props. context.pageContext.web.absoluteUrl (as it was being used elsewhere) and I was able to run this. FYI - I had to remove the console.log as it couldn't read the json twice. Thanks for your help!
    – Gaznae
    Commented Jul 9, 2018 at 8:32

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.