PnP-JS-Core library contains the number of extensible methods and properties. By using that we can achieve the various actions in a simple code. To know more about this library component, visit the below links,
Simplified JavaScript Library for SharePoint
I have explained on how to get the user collections from below articles using the same PnP library,
- Get User collection from Site Collection in Pnp-JS-Core: Gets all users from Site Collection and
- Get User collection from SharePoint Group in PnP-JS-Core: Get users from SharePoint Group
In this post I’ll explain the different options available in PnP JavaScript Library to retrive the SharePoint User from a website,
- By filter
- By User Email Id
- By User ID
- By User Login Name
By Filter:
The below example returns the user object with properties based on filter query.
$pnp.sp.web.siteUsers.filter("Title eq '<User Display Name>'").get().then(function(result) { var singleUser = result[0]; var userInfo = ""; for (prop in singleUser) { userInfo += prop + " : " + singleUser[prop] + "<br/>"; } document.getElementById("sample").innerHTML = userInfo; });
siteUsers.filter( "<Property> condition <Value>" ) returns the SharePoint user based on the filter condition from the user collection.
By User Email ID:
The below example returns the user object with properties based on User’s email id.
$pnp.sp.web.siteUsers.getByEmail("<User Email ID>").get().then(function(result) { var userInfo = ""; for (prop in result) { userInfo += prop + " : " + result[prop] + "<br/>"; } document.getElementById("sample").innerHTML = userInfo; });
siteUsers.getByEmail( "<User Email ID>" ) returns the SharePoint user based on the user email id from the user collection.
By User ID:
The below example returns the user object with properties based on User’s ID available in SharePoint site.
$pnp.sp.web.siteUsers.getById(<User ID>).get().then(function(result) { var userInfo = ""; for (prop in result) { userInfo += prop + " : " + result[prop] + "<br/>"; } document.getElementById("sample").innerHTML = userInfo; });
siteUsers.getById( <User ID> ) returns the SharePoint user based on the user id from the user collection.
By User Login Name:
The below example returns the user object with properties based on User’s login name.
//Format for Office 365 LoginName: 'i:0#.f|membership|user@sitedomain.onmicrosoft.com' $pnp.sp.web.siteUsers.getByLoginName("'<User Login Name>'").get().then(function(result) { var userInfo = ""; for (prop in result) { userInfo += prop + " : " + result[prop] + "<br/>"; } document.getElementById("sample").innerHTML = userInfo; });
siteUsers.getByLoginName( "'<User Login Name>'" ) returns the SharePoint user based on the User login name from the user collection.
To run the above examples, we have the file dependencies, PnP.js, fetch.js and promise.js. Follow the below steps for executing the examples.
- Download Required files to use PnP-JS-Core library from the below links and upload that to Site Asstes or Style Library
- Download pnp.js PnP JS file
- Download fetch.js Used by PnP js file to handle web requests and responses (Required in IE)
- Download promise.js Used by PnP js file to handle web requests and responses (Required in IE)
- Create new web part page and insert Content Editor web part
- Create a sample.html file in Site Assets or Style library and insert the example code snippet
<script type="text/javascript" src="/siteassets/scripts/fetch.js"></script> <script type="text/javascript" src="/siteassets/scripts/promise.min.js"></script> <script type="text/javascript" src="/siteassets/scripts/pnp.min.js"></script> <script type="text/javascript"> // Insert PnP example script </script>
- Add the URL of sample.html file to the content editor web part
- Click ok to apply the changes to the web part and save the page.
The post PnP-JS-Core: Get SharePoint User appeared first on Shantha Kumar's Blog - SharePoint Enthusiast.