Thursday, September 29, 2011

SharePoint Role Definition (SPRoleDefinition) Programmatically

The role definition, or permission level, is the list of rights associated with the role. For example, a user with the Read role can browse pages in the Web site and view items in lists. A role definition is a collection of rights bound to a specific object. Role definitions (for example, Full ControlReadContributeDesign, or Limited Access) are scoped to the Web site.


We can create Role definitions easily thru Web Site. But in this Blog I will tell how we can do this using programmatically.




using(SPSite site = new SPSite("http://anmol-pc"))
{
using(SPWeb web = site.OpenWeb())
{



SPRoleDefinition customRoleDef = new SPRoleDefinition();
customRoleDef.Name = "Custom-Role-Def";
customRoleDef.Description = "Can view, add and update";



customRoleDef.BasePermissions = 
SPBasePermissions.AddListItems |
SPBasePermissions.EditListItems |            
SPBasePermissions.ViewListItems | 
SPBasePermissions.OpenItems |
SPBasePermissions.ViewVersions |
SPBasePermissions.ViewFormPages |
SPBasePermissions.BrowseDirectories |
SPBasePermissions.ViewPages |
SPBasePermissions.BrowseUserInfo |
SPBasePermissions.UseRemoteAPIs |
SPBasePermissions.Open ;



//if role Definition found do nothing
if (web.RoleDefinitions.Cast<SPRoleDefinition>().Any(r => r.Name == customRoleDef.Name))
{}
else
{

//add role definition if not found
 web.RoleDefinitions.Add(customRoleDef);
 web.Update();
}



}
}

1 comment:

  1. can you explain where it can be used

    ReplyDelete

Popular Posts