Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Break Permission inheritance and add custom permission to SharePoint 2013 List using JSOM (JavaScript Client Object Model)

, 18 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Break Permission inheritance and add custom permission to SharePoint 2013 List using JSOM (JavaScript Client Object Model)

First you need to refer relevant JavaScript files.

<script type="text/javascript" src="../Scripts/jquery-1.7.1.min.js"></script>

<script type="text/ecmascript" src="http://www.codeproject.com/_layouts/SP.core.debug.js" />
<script type="text/ecmascript" src="http://www.codeproject.com/_layouts/SP.runtime.debug.js" />
<script type="text/ecmascript" src="http://www.codeproject.com/_layouts/SP.debug.js" />

Then approach is

  • Get reference to current clientcontext
  • get web site reference
  • get reference to existing list
  • break the inheritance
  • define a role
  • get the user
  • finally add user and related role to the list
<script type="text/ecmascript">
    function CustomPermision() {
        var clientContext = new SP.ClientContext.get_current();
        var oWebsite = clientContext.get_web();

        // provide the list name
        oList = clientContext.get_web().get_lists().getByTitle('ListName');
        oList.breakRoleInheritance(false, true);

        //something@sm.onmicrosoft.com
        var userobj = oWebsite.ensureUser("loginname");
        var role = SP.RoleDefinitionBindingCollection.newObject(clientContext);
        role.add(oWebsite.get_roleDefinitions().getByType(SP.RoleType.contributor));
        oList.get_roleAssignments().add(userobj, role)

       
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded),
            Function.createDelegate(this, this.onQueryFailed)
            );
    }

    function onQuerySucceeded() {
        alert("Updated");
    }

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

</script>

How to Add EveryOne (All Authenticated users) to permission group

In here you need to get the user which represent all users. Therefore you can use following code to archive that.

// All users , EveryOne , All Athenticated Users
var userobj = oWebsite.ensureUser("c:0(.s|true");
var role = SP.RoleDefinitionBindingCollection.newObject(clientContext);
role.add(oWebsite.get_roleDefinitions().getByType(SP.RoleType.contributor));
oList.get_roleAssignments().add(userobj, role)

These are the role definitions you can use:

SP.RoleType.Guest
SP.RoleType.Reader
SP.RoleType.Contributor
SP.RoleType.WebDesigner
SP.RoleType.Administrator
SP.RoleType.Editor

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Melick

Sri Lanka Sri Lanka
No Biography provided

Comments and Discussions

 
QuestionFile edit in sharepoint client object model Pinmembermanura198515-Aug-13 3:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 18 Mar 2013
Article Copyright 2013 by Melick
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid