4

I need to assign permissions to a particular list item using client object model in JavaScript.Say for 5th item by id I want to assign contribute permission to a particular group.

I have previously done this using c# object model like below. I have below c# code which can break role inheritance then assign permission.

private static void RemoveAllPermissions(SPListItem CurrentlistItem)
{

    CurrentlistItem.BreakRoleInheritance(true);       
    SPRoleAssignmentCollection SPRoleAssColn = CurrentlistItem.RoleAssignments;
    for (int i = SPRoleAssColn.Count - 1; i >=0 ; i--)
    {
        SPRoleAssColn.Remove(i);
    }
    Console.WriteLine("All Permissions Removed");
}

Then for granting permission

private static void GrantPermission(SPListItem CurrentListItem, SPWeb oSPWeb, SPRoleType SPRoleType, SPPrincipal SPPrincipal)
{

    SPRoleDefinition oSPRoleDefinition = oSPWeb.RoleDefinitions.GetByType(SPRoleType);       
     SPRoleAssignment oSPRoleAssignment = new SPRoleAssignment(SPPrincipal);         
     oSPRoleAssignment.RoleDefinitionBindings.Add(oSPRoleDefinition);         
     CurrentListItem.RoleAssignments.Add(oSPRoleAssignment);        
     CurrentListItem.Update();             
}

My question is how can this achieved by using JSOM or rest ?(any one is fine).I am using SP 2013.Any help is appreciated.

0

1 Answer 1

5

Thanks to msdn documentation it is achievable and like below for my case.

    var siteUrl = '/sites/MySiteCollection';

function breakSecurityInheritanceChangeUser() {    
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('MyList');    
    var itemId = 5;
    this.oListItem = oList.get_items().getById(itemId);    
    oListItem.breakRoleInheritance(true);    
    this.oUser = clientContext.get_web().get_siteUsers().getByLoginName('DOMAIN\\alias');    
    oListItem.get_roleAssignments().getByPrincipal(oUser).deleteObject();    
    var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);

    collRoleDefinitionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.administrator));

    oListItem.get_roleAssignments().add(oUser, collRoleDefinitionBinding);    
    clientContext.load(oUser);
    clientContext.load(oListItem);        

    clientContext.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failure));
}

function Success(sender, args) {    
    alert('Role inheritance broken for item ' + 
        this.oListItem.get_item('Title') + 
        ' and new role assignment for ' + 
        this.oUser.get_loginName());
}

function Failure(sender, args) {    
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Reference URL - MSDN documentation

1
  • @user13408 Which javascript library do I need to reference so this code can run smoothly?
    – akg1421
    Mar 22, 2020 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.