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 !…

Add a Comment

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