-1

Can someone give me the code to get the current logged in user for a spfx webpart. I don't want pnp.sp or sp.web please, I want to use spHttpClient etc.

I cannot find the basic code ANYWHERE on the internet. A link will do.

Regards,

T

NightTom
  • 418
  • 15
  • 37

1 Answers1

0

Sample code:

import { SPHttpClient, SPHttpClientResponse,ISPHttpClientOptions} from '@microsoft/sp-http';    
this.context.spHttpClient.get(`${this.context.pageContext.web.absoluteUrl}/_api/SP.UserProfiles.PeopleManager/GetMyProperties`,
      SPHttpClient.configurations.v1).then((response: SPHttpClientResponse) =>{
        response.json().then((responseJSON: any) => { 
          console.log(responseJSON.DisplayName)
         })
      })
Lee
  • 5,305
  • 1
  • 6
  • 12
  • Getting an error of 0: Unable to get property 'get' of undefined or null reference – NightTom Dec 02 '19 at 12:42
  • null object reference, try to debug which object is null. https://learn.microsoft.com/en-us/sharepoint/dev/spfx/debug-in-vscode – Lee Dec 04 '19 at 02:50