Do you want to break the permission inheritance in SharePoint using JSOM?

Code snippets helps you to break the permission inheritance in SharePoint for the list, list item and subsite.

I have recently received the question related to the permission inheritance. “How to break the permission for the list item using SharePoint JSOM model”. Based on that I have provided the JSOM snippets for breaking the inheritance and add a new permission to the group for Web, List and List Item.

How to break the permission for the List using SharePoint JSOM model

The below code breaks the inheritance to the SharePoint list “TestList” and assign contribute permission to the “ktskumar Visitors” in current SharePoint site.

[code lang=”js”]
function breakListPermission(listName, groupName) {

var clientContext = new SP.ClientContext();
oList = clientContext.get_web().get_lists().getByTitle(listName);

//Below line breaks the List permission inheritance
oList.breakRoleInheritance(false);

//Bind Contribute permission to the Group
oGroup = clientContext.get_web().get_siteGroups().getByName(groupName);
var groupPermissionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);
groupPermissionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.contributor));

//Assign binded permission to the page or listitem
oList.get_roleAssignments().add(oGroup, collRoleDefinitionBinding);

clientContext.load(oList);
clientContext.executeQueryAsync(
Function.createDelegate(this, function() {
alert(‘Applied the unique permission to the list and assign contribute permission to the group’);
}),
Function.createDelegate(this, function(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}));
}

function injectMethod() {
breakListPermission("TestList","ktskumar Visitors");
}
ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");
[/code]


Note:

breakRoleInheritance(false) – Breaks the inheritance and removes all assignation. And then it adds the full control permission to the current user.

breakRoleInheritance(true) – Breaks the inheritance and it persists the all assignation as it is. You can manually change the permission here afterwards.


HOW TO BREAK THE PERMISSION FOR THE LISTITEM USING SHAREPOINT JSOM MODEL

The below code breaks the inheritance to the SharePoint list item under “TestList” and assign contribute permission to the “ktskumar Visitors” in current SharePoint site.

[code lang=”js”]
function breakItemPermission(listName, listItemID, groupName) {

var clientContext = new SP.ClientContext();
oList = clientContext.get_web().get_lists().getByTitle(listName);
oListItem = oList.getItemById(listItemID);

//Below line breaks the ListItem permission inheritance
oListItem.breakRoleInheritance(false);

//Bind Contribute permission to the Group
oGroup = clientContext.get_web().get_siteGroups().getByName(groupName);
var groupPermissionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);
groupPermissionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.contributor));

//Assign binded permission to the page or listitem
oListItem.get_roleAssignments().add(oGroup, collRoleDefinitionBinding);

clientContext.load(oListItem);

clientContext.executeQueryAsync(
Function.createDelegate(this, function() {
alert(‘Applied the unique permission and assigns contribute permission to the group’);
}),
Function.createDelegate(this, function(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}));
}

function injectMethod() {
breakItemPermission("TestList",2,"ktskumar Visitors");
}
ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");
[/code]


HOW TO BREAK THE PERMISSION FOR THE current subsite USING SHAREPOINT JSOM MODEL

The below code breaks the inheritance to the current website and assign the contribute permission to the “ktskumar Visitors“.

[code lang=”js”]

function breakWebPermission(groupName) {

var clientContext = new SP.ClientContext();
oWeb = clientContext.get_web();

//Below line breaks the current site (subsite) permission inheritance
oWeb.breakRoleInheritance(false);

//Bind Contribute permission to the Group
oGroup = clientContext.get_web().get_siteGroups().getByName(groupName);
var groupPermissionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);
groupPermissionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.contributor));

//Assign binded permission to the current website
oWeb.get_roleAssignments().add(oGroup, collRoleDefinitionBinding);

clientContext.load(oWeb);

clientContext.executeQueryAsync(
Function.createDelegate(this, function() {
alert(‘Applied the unique permission and assigns contribute permission to the group’);
}),
Function.createDelegate(this, function(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}));
}

function injectMethod() {
breakWebPermission("ktskumar Visitors");
}
ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");
[/code]

The code snippets from this blog helps to break the inheritance to the subsite, list and listitem or document or page.

Shantha Kumar
Shantha Kumar
Articles: 280