How to Get Current logged in user id in SPFx using React Typescript

Manimekalai
 
Technology Specialist
March 21, 2018
 
Rate this article
[Total: 0    Average: 0/5]
 
Views
5004

We can’t use _spPageContextInfo inside a SPFx webpart, hence to get the current logged-in user name, email etc except user id please make use of below syntax.

this.context.pageContext.user doesn’t include id or userId

When I don’t want to make a rest call or other methods to get the current user Id!!!

Here is the solution

Add import pnp from ‘sp-pnp-js’; in the top of webpart.ts file

clip_image002

Use the node.js command prompt to install below command to avoid the missing module error

npm install sp-pnp-js –save

Add the below code to your webpart.ts file

var email=this.context.pageContext.user.email;

this. getUserId (email);

public getUserId(email: string): Promise<number> {

return pnp.sp.site.rootWeb.ensureUser(email).then(result => {

return result.data.Id;

});

}

clip_image004

Thank you!

Category : React, SharePoint, SPFx

Author Info

Manimekalai
 
Technology Specialist
 
Rate this article
[Total: 0    Average: 0/5]
 
Having 6 years of experience in design and development of SharePoint applications, Mani has expertise in developing applications using Office 365, SharePoint 2013 & 2010, SPFX, SP Designer Workflows, Visual ...read more
 

Leave a comment