Here let us look at the working with SharePoint site
permissions using the PnP JavaScript library. Some of the basic operations like
retrieve available permissions, add or remove the user permissions from the
site will be explained.
Note:
- PnP JavaScript Core Library is supported on SharePoint 2013, SharePoint 2016 and SharePoint Online versions.
- The prerequisites for the operations are es6-promise.js, fetch.js, pnp.js or pnp.min.js.
The following sample shows us retrieving all user
permissions of the SharePoint site along with the user roles using PnP
JavaScript Core library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function GetUserPermissionById(userid){ | |
$pnp.sp.web.roleAssignments.getById(userid).expand('RoleDefinitionBindings').get().then(roleAssignments => { | |
var roles = roleAssignments.RoleDefinitionBindings; | |
roles.forEach(function(roleDef) { | |
console.log("Permission: " + roleDef.Name); | |
// Append the result to your html | |
}, this); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function GetUserPermissions() { | |
$pnp.sp.web.roleAssignments.expand('Member', 'RoleDefinitionBindings').get().then(users => { | |
users.forEach(function (element) { | |
// console.log("User: " + element.Member.Title); | |
// Append the result to your html | |
var roles = element.RoleDefinitionBindings; | |
roles.forEach(function(roleDef) { | |
console.log("Permission: " + roleDef.Name); | |
// Append the result to your html | |
}, this); | |
}, this); | |
}); | |
} |