Thursday 28 December 2017

Working with SharePoint Site Permissions using PnP JavaScript Core Library

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 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.




  • 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.


  • 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