Getting Current user properties using PNP JS

In this article. I have explained how to get current user properties of the SharePoint user using pnp js.

Introduction:

PNP means Patterns and Practices is a JavaScript library to accomplish SharePoint operations in more productive way for both SharePoint online and on-premise versions

In previous REST API Calls we fetch the current user properties using the below endpoint through AJAX

Endpoint;

Method: GET

http://<SiteCollectionUrl>/_api/web/currentuser

Now let see how to done the same using PNP JS in the simplest way.

Import the PNP library from node

import pnp from "@pnp/pnpjs"; 

pnp.sp.web.currentUser.get().then(f => {
  console.log("user", f);
})

It will return the successful response like below in JSON format

you can also use same JavaScript library of PNP Js in your normal JavaScript projects like Content editor webparts like below snippet

//Directly called library form CDN for reference

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/sp-pnp-js/3.0.10/pnp.min.js"/>

$pnp.sp.web.currentUser.get().then(function(response){
  console.log("user", response);
});

Download it form CDN.

In my upcoming blogs let discuss more operations using PNP JS

Happy SharePointing !…

Author Profile

Vinodh
Vinodh is a Microsoft MVP for SharePoint (Office Apps and Services) having 5+ years of experience in Microsoft Technologies.

His interest and expertise includes SharePoint Online Development, PowerApps Development, Flows for SharePoint and PowerApps. Single Page Apps/Add ins for SharePoint using React, Angular. SharePoint Farm Management, 2, 3 and upto 6 tier SharePoint Farm Installation and Configuration.

Awards :
Microsoft Most Valuable Professional for SharePoint and Office 365 awarded for the year 2016, 2017 and 2018.
C# Corner Most Valuable Professional for SharePoint and Office 365 awarded for the year 2016, 2017 and 2018.
Technology Expertise :
SharePoint Online, SharePoint 2013, SharePoint 2016 and SharePoint 2019 Preview.
Intranets both Modern and Classic, SharePoint Frameworks Customisation, SharePoint Add-ins, SharePoint single page apps.
Angular 5 with Bootstrap single page apps.
Typescript, JQuery and Javascript.
Micrsoft Flow and Powerapps.
Utilised :
Microsoft Graph API, SharePoint REST, SharePoint PnP JS.
Out of Box :
Consume Content search web parts for internet publishing sites using custom display templates.
Having knowledge on SharePoint workflows and SharePoint Designer workflows.
List customisation using JS Link, JSON Formatting.
Having knowledge in Active directory management services.
Create and configure service applications in SharePoint (MMS, UPSA, SSA).
Configure Outgoing email settings using SMTP Services.

Add a Comment

Your email address will not be published. Required fields are marked *