Creating RoleDefinitions, UserGroups and RoleDefinitionBindings


  1. Create a RoleDefinition as per the permissions you would like that role to have. I would create "Reader " with below roledefinition
       1: SPRoleDefinition readersRoleDefinition = new SPRoleDefinition()

       2:{

       3:	Name = "Readers",

       4:	Description = "My site collection Readers",

       5:	BasePermissions =

       6:	SPBasePermissions.ViewListItems |

       7:	SPBasePermissions.OpenItems |

       8:	SPBasePermissions.ViewVersions |

       9:	SPBasePermissions.CreateAlerts |

      10:	SPBasePermissions.ViewFormPages |

      11:	SPBasePermissions.ViewPages |

      12:    SPBasePermissions.AddAndCustomizePages |

      13:	SPBasePermissions.BrowseUserInfo |

      14:	SPBasePermissions.UseRemoteAPIs |

      15:	SPBasePermissions.UseClientIntegration |

      16:	SPBasePermissions.Open

      17: 

      18:};

  2. Add the RoleDefinition if it doesn’t exist in the root web .
       1: // add the role definition for readers

       2:bool isNotFound = true;

       3:foreach (SPRoleDefinition roleDef in rootWeb.RoleDefinitions)

       4:{

       5:	if (roleDef.Name.Equals("Readers"))

       6:	{

       7:		isNotFound = false;

       8:		break;

       9:	}

      10:}

      11:if (isNotFound)

      12:{

      13:	rootWeb.RoleDefinitions.Add(readersRoleDefinition);

      14:}

  3. Create a Group with below syntax . I have created "Readers" for my site collection and have added a couple of users on my VM
       1: rootWeb.SiteGroups.Add(

       2:     "Readers",

       3:     rootWeb.SiteUsers[@"MOSS2007\Administrator"],

       4:     rootWeb.SiteUsers[@"MOSS2007\Administrator"],

       5:     "Site Collection Readers Group");

       6: 

       7: rootWeb.SiteGroups["Readers"].AddUser(@"MOSS2007\Jay", string.Empty, "Jay Dennis", string.Empty);

       8: rootWeb.SiteGroups["Readers"].AddUser(@"MOSS2007\Mila", string.Empty, "Mila Wang", string.Empty);

  4. Finally Create an SPRoleAssignment and assign the RoleDefinition to the Group which you created and all the users in that group would have the SPBasePermissions defined in the RoleDefinition.
       1: SPRoleAssignment roleAssignmentReaders = new SPRoleAssignment(rootWeb.SiteGroups["Readers"]);

       2: roleAssignmentReaders.RoleDefinitionBindings.Add(rootWeb.RoleDefinitions["Readers"]);

       3: rootWeb.RoleAssignments.Add(roleAssignmentReaders);

Advertisements

Tags: ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: