Creating SharePoint 2010 permission level programmatically

If you have many sites under a web application, there will be a situation when you need to use the custom permission for a particular site. Some time you inherit the roles and sometime not. You can create custom permission level programmatically.

You can only create new role (permission level) at site collection level, and then you can use this role in sub sites.

Here is the code to create new permission level.

Add assembly reference:

using Microsoft.SharePoint;
 

//Code:

     SPSite site = new SPSite(SPContext.Current.Site.Url);
     SPWeb web = site.OpenWeb();
    if(web.HasUniqueRoleDefinitions || web.IsRootWeb)
    {
        try
        {
            web.AllowUnsafeUpdates = true;
            SPRoleDefinition roleDef = new SPRoleDefinition();
            roleDef.BasePermissions = SPBasePermissions.ManageLists |
            SPBasePermissions.CancelCheckout |       
                 SPBasePermissions.AddListItems |
            SPBasePermissions.EditListItems |
            SPBasePermissions.DeleteListItems;
            roleDef.Name = "EblogIn Role";
           web.RoleDefinitions.Add(roleDef);
           web.Update();
  }
  catch (Exception ex) { }
  }

More Blogs

Contact us

To begin your digital transformation, get in touch.

We’re pleased to address any inquiries you might have and assist you in selecting the service that best suits your requirements.

Your benefits:
Speak To Us