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|firstname.lastname@contoso.onmicrosoft.com

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



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|firstname.lastname@contoso.onmicrosoft.com      

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
    Jul 6 '18 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. Jul 6 '18 at 18:10
  • I wrote this inside the render method of the webpart typescript (.ts) file. Jul 7 '18 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
    Jul 9 '18 at 8:32

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.