Thursday, May 19, 2016

Set list item level permission in jsom (javascript object model)

ar siteUrl = '/sites/peakfinder';

function breakSecurityInheritanceChangeUser() {    
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Employee');    
    var itemId = 5;
    this.oListItem = oList.get_items().getById(itemId);    
//Break Role Inheritance
    oListItem.breakRoleInheritance(true);    
//Get user from the site
    this.oUser = clientContext.get_web().get_siteUsers().getByLoginName('peakfinder\\testuser1');    
    oListItem.get_roleAssignments().getByPrincipal(oUser).deleteObject();    
    var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);

    collRoleDefinitionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.administrator));
//assign the user
    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());
}

No comments:

Post a Comment