How to Get SharePoint Folder Permissions Using JavaScript?

Get folder permission using JavaScript in SharePoint

In this post, we will learn how to get SharePoint folder permissions using JavaScript (JSOM). Also, we will learn how to retrieve only all assigned groups for a particular folder as well as we will list all the assigned users for a specific folder in a SharePoint document library using JSOM.

get unique permissions for a folder in SharePoint using JSOM

Get SharePoint folder permissions using JavaScript

To get the unique permissions for a specific folder in a SharePoint document library, you should use get_roleAssignments() function. The get_roleAssignments() will retrieve all assigned users and groups to a specific item in SharePoint. So let’s go first through a real example.

Consider, you have a document library in SharePoint 2016 that has some folders,

Get SharePoint folder permissions using JavaScript

One of these folders has a unique permissions as shown below:

get unique permissions for a folder in SharePoint using JSOM

And you need to use JavaScript (JSOM) to do the following:

  • Get all unique permissions for a particular folder in this SharePoint document library.
  • Get all assigned groups for a particular folder in this SharePoint document library.
  • Get all assigned users for a particular folder in this SharePoint document library.

How to get unique permissions for a folder in SharePoint using JSOM?

To retrieve all unique permissions for a specific folder in SharePoint document library using JavaScript, you should do the following:

  1. Download the tested JS script from GitHub at Getting unique permissions for a folder.
  2. Provide the document library name at the below line.
    var docLib = ctx.get_web().get_lists().getByTitle('Document Library Name');
  1. Again, Provide the folder name that you need to get all assigned groups and users at this line.
if (curruntFolder.get_displayName() == "Folder Name") {
  1. After providing your own document library and folder name, you can test the JS in an script editor web part.
Add Script Editor Web Part
  1. Stop editing the page, and reload it, then hit refresh.
  2. You will note that the assigned users and groups for provided folder is listed as shown below:
get all assigned permissions for a folder using JS
get SharePoint folder permissions

How to get only all assigned groups for a folder using JavaScript in SharePoint?

In the above example, we have explained how to get SharePoint folder permissions (Users and Groups). In this example, we will learn how to only retrieve all assigned groups for a particular folder in SharePoint document Library excluding User’s permissions.

To can check if the current permission type is a group or a user, you should use get_principalType() that holds integer values for each permission type as the following:

TypeValue
User1
Distribution List2
Security Group4
SharePoint Group8

Based on the above table, we should add an additional condition to check if the current permission type is a group or not. so we should check if get_principalType() = 8 which means it will only retrieve the permissions with type Groups.

var permissionType = currentRole.get_member().get_principalType();
var permissionName = currentRole.get_member().get_loginName();
     if ( permissionType == 8)
            alert(permissionName);
get all group permissions for a folder using JSOM
get SharePoint folder permissions (Only Groups)

Download the full script from GitHub at Only Get all Assigned Groups for a SharePoint Folder.

Please, Don’t forget to update the document library and folder name as we have earlier mentioned.

How to get only all assigned users for a folder using JavaScript in SharePoint?

Now, it’s clear, we just need to check if get_principalType() = 1 that means it will only retrieve the permissions with type Users.

var permissionType = currentRole.get_member().get_principalType();
var permissionName = currentRole.get_member().get_loginName();
     if ( permissionType == 1)
            alert(permissionName);
get all user permissions for a folder using JavaScript

Download the full script at Getting all Assigned Users for a Folder in SharePoint Document Library

Please, Don’t forget to update the document library and folder name as we have earlier mentioned.


Conclusion

In conclusion, we have provided three JS scripts to get SharePoint folder permissions per Permission Type (Groups and Users)

Applies To
  • SharePoint 2019.
  • SharePoint 2016.
  • SharePoint 2013.
  • SharePoint Online.
Download
See Also

1 thought on “How to Get SharePoint Folder Permissions Using JavaScript?”

Leave a Reply

Scroll to Top