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.
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,
One of these folders has a unique permissions as shown below:
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.
To retrieve all unique permissions for a specific folder in SharePoint document library using JavaScript, you should do the following:
- Download the tested JS script from GitHub at Getting unique permissions for a folder.
- Provide the document library name at the below line.
var docLib = ctx.get_web().get_lists().getByTitle('Document Library Name');
- Again, Provide the folder name that you need to get all assigned groups and users at this line.
if (curruntFolder.get_displayName() == "Folder Name") {
- After providing your own document library and folder name, you can test the JS in an script editor web part.
- Stop editing the page, and reload it, then hit refresh.
- You will note that the assigned users and groups for provided folder is listed as shown below:
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:
Type | Value |
---|---|
User | 1 |
Distribution List | 2 |
Security Group | 4 |
SharePoint Group | 8 |
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);
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.
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);
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
- Download the full scripts from GitHub at Getting unique permissions for a folder in SharePoint using JSOM.
How would you go about also capturing the users permission level?