How to get current user details in sharepoint custom webparts?

Problem Statement

How to capture the user details like user id and user email id, user login name, and user office in Sharepoint web part?

Solution

Step 1

  • Do npm install sp-pnp-js

Step 2

  • Go to the component file and put the import { Web } from “sp-pnp-js”;

Step 3

  • Create a method and make a rest call by using sp-pnp-js, below is the sample code for the same.
public getUserDetails(){
 
    let web = new Web(this.props.context.pageContext.web.absoluteUrl);  
    return web.currentUser.get().then((user) => {
     return user;
     });
}

You can call this method and store the returned user data from this method and use it.

Step 4

  • You can access this user information by user.Email , user.Email.

This will help you to get the user details.

Thank you

1 Like