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#.
Final output you can check from the site. It will look like below:

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.
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).

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