Shantha Kumar T
PnP-JS-Core: Get users from SharePoint Group
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
This post explains,
How to get all users from a SharePoint group using PnP JavaScript Library.
Example:
The below steps and code snippet used to get all the users from a given group in SharePoint site using PnP JavaScript library,
- 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 below code snippet
[code language=”javascript”]
<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><div id="sample"></div>
<script type="text/javascript">
//siteGroups.getByName returns the SiteGroup object based on the given group name
//users property returns all users object in an array
//then( ) runs on success
//catch( ) runs on failure
//Replace <Group Name> with the actual group name$pnp.sp.web.siteGroups.getByName(‘<Group Name>’).users.get().then(function(result) {
var usersInfo = "";
for (var i = 0; i < result.length; i++) {
usersInfo += "Title: " + result[i].Title + " ID:" + result[i].Id + "<br/>";
}
document.getElementById("sample").innerHTML = usersInfo;
}).catch(function(err) {
alert("Group not found: " + err);
});
</script>
[/code] - 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.
- Now the page displays a user’s display name and id from a given SharePoint group using PnP JavaScript method .