Tag Archives: EventReceiver

Creating SharePoint groups programmatically in feature event receiver

The following snippet shows an example of how to add SharePoint groups to a site and set their permission levels. The code is run by adding it to an event receiver for a feature, inside the overridden FeatureActivated method.

 

 public class MBL_PermissionsEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties.Feature.Parent is SPWeb)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(((SPWeb)properties.Feature.Parent).Url))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;

                            if (!web.HasUniqueRoleAssignments)
                            {
                                web.BreakRoleInheritance(false);
                            }

                            web.Update();

                            SPUserCollection users = web.AllUsers;
                            SPUser member = users[web.Site.RootWeb.SiteAdministrators[0].LoginName];
                            SPMember owner = web.SiteGroups["Owner of " + web.Title];
                            SPRoleAssignment roleAss = null;
                            SPGroup group = null;
                            SPRoleDefinitionCollection roles = web.RoleDefinitions;
                            SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;
                            SPRoleDefinitionBindingCollection roleDefBindings = null;

                            web.SiteGroups.Add("AdminGroup", owner, member, "");

                            web.Update();

                            SPMember ownerSubGroups = web.SiteGroups["AdminGroup"];
                            
                            web.SiteGroups.Add("group1", ownerSubGroups, member, "");
                            web.SiteGroups.Add("group2", ownerSubGroups, member, "");
                            web.Update();

                            group = web.SiteGroups["Owner of " + web.Title];
                            roleAss = new SPRoleAssignment(group);
                            roleDefBindings = roleAss.RoleDefinitionBindings;
                            roleDefBindings.Add(roles["Full Control"]);
                            roleAssignments.Add(roleAss);

                            web.Update();

                            group = web.SiteGroups["AdminGroup"];
                            roleAss = new SPRoleAssignment(group);
                            roleDefBindings = roleAss.RoleDefinitionBindings;
                            roleDefBindings.Add(roles["Full Control"]);
                            roleAssignments.Add(roleAss);

                            web.Update();

                            group = web.SiteGroups["group1"];
                            roleAss = new SPRoleAssignment(group);
                            roleDefBindings = roleAss.RoleDefinitionBindings;
                            roleDefBindings.Add(roles["Contribute"]);
                            roleAssignments.Add(roleAss);

                            web.Update();

                            group = web.SiteGroups["group2"];
                            roleAss = new SPRoleAssignment(group);
                            roleDefBindings = roleAss.RoleDefinitionBindings;
                            roleDefBindings.Add(roles["Read"]);
                            roleAssignments.Add(roleAss);

                            web.Update();
                        }
                    }
                });
            }
        }
    }
Advertisements