Programmatically create custom role in selected sub sites in SharePoint 2010

Posted By:Jayasankar       Posted Date: May 04, 2004       Category: SharePoint    URL: http://www.dotnetspark.com


In this article I am explaining how to create a custom role in Sharepoint using C#.




In this article I am explaining how to create a custom role in SharePoint using C#.

A custom role is required in SharePoint when you need to assign a role to the user as per the requirement not the OOTB.

Add this code to your solution to add the custom role based on the sub site.

I have the following scenrio:

Person: People picker that will be used to add a user.

Country: Listbox containing the list of subsites where we will add the user and assign roles.

Administrator type: Type of custom role where we will assign to the selected user in selected susbsite (Country).

share1.gif

Code :
001private void AddUserToGroup(string selectedUser, string userGroupName,string subSite)
002{
003SPSecurity.RunWithElevatedPrivileges(delegate
004{
005using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
006{
007using (SPWeb spWeb = spSite.AllWebs[subSite
008{
009try
010{
011spWeb.AllowUnsafeUpdates = true;
012SPUser spUser = spWeb.EnsureUser(selectedUser);
013spWeb.RoleDefinitions.BreakInheritance(true, true);
014SPRoleDefinition role;
015switch(userGroupName)
016{
017case "Super Administrator":
018role = new SPRoleDefinition
019{
020Name = userGroupName,Description = userGroupName,BasePermissions = SPBasePermissions.FullMask ^
021(SPBasePermissions.ManagePermissions
022SPBasePermissions.ManageLists
023SPBasePermissions.AddListItems
024SPBasePermissions.EditListItems
025SPBasePermissions.DeleteListItems
026SPBasePermissions.ViewVersions
027SPBasePermissions.DeleteVersions
028SPBasePermissions.CreateAlerts
029SPBasePermissions.CreateGroups)
030};
031break;
032case "Regional Administrator":
033role = new SPRoleDefinition
034{
035Name = userGroupName,Description = userGroupName,BasePermissions = SPBasePermissions.FullMask ^
036(SPBasePermissions.ManagePermissions
037SPBasePermissions.ManageLists
038SPBasePermissions.AddListItems
039SPBasePermissions.EditListItems
040SPBasePermissions.DeleteListItems
041SPBasePermissions.ViewVersions
042SPBasePermissions.DeleteVersions
043SPBasePermissions.CreateAlerts
044)
045};
046break;
047case "Marketing Administrator":
048role = new SPRoleDefinition
049{
050Name = userGroupName,Description = userGroupName,BasePermissions = SPBasePermissions.FullMask ^
051(SPBasePermissions.ManagePermissions
052SPBasePermissions.ManageLists
053SPBasePermissions.AddListItems
054SPBasePermissions.EditListItems
055SPBasePermissions.DeleteListItems
056SPBasePermissions.ViewVersions
057SPBasePermissions.DeleteVersions
058)
059};
060break;
061case "Country Administrator":
062role = new SPRoleDefinition
063{
064Name = userGroupName,Description = userGroupName,BasePermissions = SPBasePermissions.FullMask ^
065(SPBasePermissions.ManagePermissions
066SPBasePermissions.ManageLists
067SPBasePermissions.AddListItems
068SPBasePermissions.EditListItems
069SPBasePermissions.DeleteListItems
070)
071};
072break;
073default:
074role = new SPRoleDefinition
075{
076Name = userGroupName,Description = userGroupName,BasePermissions = SPBasePermissions.FullMask ^
077(SPBasePermissions.ManagePermissions
078SPBasePermissions.ManageLists
079SPBasePermissions.AddListItems
080 
081};
082break;
083}
084spWeb.RoleDefinitions.Add(role);
085spWeb.Update();
086spWeb.RoleDefinitions.Cast<sproledefinition>().First(def => def.Name == userGroupName);
087SPRoleDefinition newrole = spWeb.RoleDefinitions[userGroupName];
088SPRoleAssignment roleAssignment;
089roleAssignment = new SPRoleAssignment(spUser.LoginName, spUser.Email, spUser.Name, "Notes about user");
090roleAssignment.RoleDefinitionBindings.Add(newrole);
091spWeb.RoleAssignments.Add(roleAssignment);
092spWeb.Update();
093lblError.Text = selectedUser + " is added to the " + userGroupName + "in subsite " + spWeb.Title;
094}
095catch (Exception ex)
096{
097lblError.Text = ex.Message;
098}
099finally
100{
101spWeb.AllowUnsafeUpdates = false;
102}
103}
104}
105});
106}
107</sproledefinition>

Final output you can check from the site. It will look like below:

share2.gif

That's it for now.

I created the component as a webpart. You can use the same code in a handler/workflow code activity etcâ?¦

Hope you like this article. It's really helpful when you come across a concept when permission management is needed for your SharePoint component.

Hope this article will save you a lot of time and effort.

Comments