Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Friday, December 02, 2011

Create Custom Permission level using Client Object model


ClientContext ctx = new ClientContext(“SPSiteurl”);

//create a new base permission

BasePermissions perms = new BasePermissions();

var _basePerm = | SPBasePermissions.AddListItems| SPBasePermissions.EditListItems;

perms.Set((PermissionKind)Enum.Parse(typeof(PermissionKind), _basePerm));

//create the construct for a new role definition

RoleDefinitionCreationInformation rdInfo = new RoleDefinitionCreationInformation();

//set the perms

rdInfo.BasePermissions = perms;

//set a descriptionrdInfo.Description = “Custom Role definition created with the client object
model”;

//set the name

rdInfo.Name = “My custom Permission level”;

//add the definition to the web collection

RoleDefinition rd = ctx.Web.RoleDefinitions.Add(rdInfo);

//execute to create

ctx.ExecuteQuery();

Now Assign a user this custom role definition –

/get the group

RoleDefinition rd = ctx.Web.RoleDefinitions.GetByName(“My custom Permission level”);

//get the user object

Principal usr = ctx.Web.EnsureUser(“domain\isha”);

//create the role definition binding collection

RoleDefinitionBindingCollection rdb = new RoleDefinitionBindingCollection(ctx);

//add the role definition to the collection

rdb.Add(rd);

//create a RoleAssigment with the user and role definition

ctx.Web.RoleAssignments.Add(usr, rdb);

No comments:

Post a Comment