SharePoint recommends assigning role-based permissions. All permissions are managed through roles. Roles are classified into two sections,
- Role Definition and
- Role Assignment
Role definition, also known as permission level, is the list of permissions associated with the role. Full control, contribute, read, design, and limited access are some of the role definitions available. Role assignment is the relationship established between users/groups and the role definition. Hence when we assign a role programmatically, it is a two-step process: instantiation of role definition and implementation of role assignment to the user/group.
Let’s see how we can add a role to user/group using JavaScript object model.
- Add reference to jquery file,
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- Within the document ready function call SP.SOD.executeFunc so as to load the on demand script SP.js . Call the main starting point function, say: addRole.
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', addRole);
- Instantiate client context and get the web instance. Once the web object is retrieved, get the group to be assigned the role,
- var clientContext = new SP.ClientContext();
- var oWeb = clientContext.get_web();
- //Get the group to be assigned the role
- var oGroup= oWeb.get_siteGroups().getByName("HR Group");
- Get role definition and role definition binding collection,
- var oRoleDef = oWeb.get_roleDefinitions().getByName('Contribute');
- var oBindingColl = SP.RoleDefinitionBindingCollection.newObject(clientContext);
- // Add the role to the collection.
- oBindingColl.add(oRoleDef);
- Get the role assignment collection for the target web and assign the group to the new role definition binding collection.
- var oCurrentRoleAssignments = oWeb.get_roleAssignments();
- var roleAssignmentContribute = oCurrentRoleAssignments.add(oGroup, oBindingColl);
- Load the client context and execute the batch which will send the request to the server and perform the entire JavaScript object model operation as a batch.
- clientContext.load(oGroup);
- clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
Output

Full Code




Santhakumar MunuswamyPosted Jun 25, 2016, 3:10 AM
Nice share
Kuppurasu NagarajPosted Jun 14, 2016, 1:16 PM
Nice Sharing..
Vignesh ManiPosted Jun 14, 2016, 12:15 PM
Nice
RajaPosted Jun 13, 2016, 12:33 AM
Nice Sharing....
Vinodh NarayananPosted Jun 13, 2016, 12:14 AM
good one
Debasis SahaPosted Jun 13, 2016, 12:14 AM
Nice share..