top of page

Working with SharePoint Site Permissions using PnP JavaScript Core Library

Updated: Mar 29, 2019

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.jsfetch.js, pnp.js or .

The following sample shows us retrieving all user permissions of the SharePoint site along with the user roles using PnP JavaScript Core library.

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);

});}



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);

});}

  • GetUserPermissions() pulls all user permissions from the site.GetUserPermissionById(userid) - retrieves the users of the site with permissions.

  • User ID will be the principle ID (integer) of the user on the SharePoint site.

The following sample shows adding or removing user permissions to/from the SharePoint site along with the user roles using PnP JavaScript Core library.

function AddUserToSitePermission(userid,roledefid)

{

$pnp.sp.web.roleAssignments.add(userid,roledefid).then(permission =>

{

console.log(permission);

// Append the result to your html

});}


function RemoveUser(userid,roledefid)

{

$pnp.sp.web.roleAssignments.remove(userid,roledefid).then(permission =>

{

console.log(permission);

// Append the result to your html

});}

  • AddUserToSitePermission(userid,roledefid) - Add user to the site, where userid is principle user of id and roledefid will be permission level value.

  • RemoveUser(userid,roledefid) - Removes user from the site with the permission.

For example, following code to be executed to add the user (userid:74) to the site with full control permission.

AddUserToSitePermission(74, 1073741829)


The following table shows the role ID of the permission levels. Permission Role Definition Value

Full Control 1073741829

Contribute 1073741827

Read 1073741826

0 comments
bottom of page