Get Current User Information Using PnP Library In SharePoint Framework

Here, you'll learn on how to get the current user information in SharePoint Framework web part using PnP JavaScript library.

PnP JavaScript library contains a lot of methods and properties for accessing SharePoint Data. We will see how to use this library in SharePoint Framework to get the current user information.

Use the below command to create a SharePoint Framework project,

yo @microsoft/sharepoint

Create a SharePoint Framework project with “No JavaScript” option. So the below code snippet does not have any JavaScript Frameworks and you can reuse it in any JavaScript framework APIs.

After the project creation, use the below npm command to install the PnP JavaScript dependencies to the SharePoint Framework project.

npm install –save @pnp/common @pnp/sp @pnp/logging @pnp/odata

After importing the PnP JS dependencies, import the classes from PnP JS to the web part by adding below lines in the _webpart.ts file.

  1. import { sp } from "@pnp/sp";  
  2. import { CurrentUser } from '@pnp/sp/src/siteusers'

 So the object is the starting point for accessing all the SharePoint related PnP objects and methods.

CurrentUser object returns the following properties for now. In the future, it may change based on the new properties introduced to the SharePoint and open source contributions to this library. We have to import CurrentUser object from SiteUsers class from the @pnp/sp.
  • Email: string;
  • Id: number;
  • IsHiddenInUI: boolean;
  • IsShareByEmailGuestUser: boolean;
  • IsSiteAdmin: boolean;
  • LoginName: string;
  • PrincipalType: number;
  • Title: string;

Add the below code snippet under the _WebPart class in _webpart.ts file in the project. getSPData method calls the PnP request method to fetch the current users' data from SharePoint. renderData method is used to generate the HTML and pass the value for rendering. Render method renders the HTML in the browser.

  1. //Get Current User Display Name  
  2. private getSPData(): void {      
  3.   sp.web.currentUser.get().then((r: CurrentUser) => {  
  4.     this.renderData(r['Title']);  
  5.   });  
  6. }  
  7.    
  8. private renderData(strResponse: string): void {  
  9.   const htmlElement = this.domElement.querySelector("#pnpinfo");  
  10.   htmlElement.innerHTML = strResponse;  
  11. }  
  12.    
  13. public render(): void {  
  14.   this.domElement.innerHTML = `  
  15.     <div class="${ styles.pnPDemos}">  
  16.       <div class="${ styles.container}">  
  17.         <div class="${ styles.row}">  
  18.           <div class="${ styles.column}">  
  19.             <div id="pnpinfo"></div>  
  20.           </div>  
  21.         </div>  
  22.       </div>  
  23.     </div>`;  
  24.   this.getSPData();  

Below is the output based on the code snippet. This returns the current user title value. We can also use other user properties to display in the webpart.

Get Current User Information Using PnP Library In SharePoint Framework

Get Groups of Current User

Based on the current user, we can also get the groups where the current user is the member. CurrentUser.Groups return the collection of SharePoint Groups associated with the current user. Replace the getSPData method with the following code, which is used to fetch the groups associated by the current user in SharePoint site.

  1. //Get collection of SharePoint Groups for the current User  
  2. private getSPData(): void {      
  3.   sp.web.currentUser.groups.get().then((r: any) => {  
  4.     let grpNames: string ="";  
  5.     r.forEach((grp: SiteGroup) =>{  
  6.       grpNames += "<li>"+grp["Title"]+"</li>"  
  7.     });      
  8.     grpNames = "<ul>"+grpNames+"</ul>";  
  9.     this.renderData(grpNames);  
  10.   });  

I hope, you have learned how to access the current user details in SharePoint Framework webpart using PnP JavaScript Library.