Sunday, 2 December 2012

Programmatically how to assign,change and delete pred-defined roles to groups in SharePoint 2010

In my previous post shown how to create and delete groups. Now, i wanna show how to assign role to groups.

Create group and assign pre-defined role to SharePoint group:

Here am creating sample Test Group and assign contributor role to this group.

            using (SPSite site = new SPSite("http://UKREDDY:6969/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.SiteGroups.Add("TestGroup", web.CurrentUser, web.CurrentUser, "This is Test Group created.");
                    //Getting newly created group
                    SPGroup group = web.SiteGroups["TestGroup"];
                    //Getting Pre-Defined Roles
                    SPRoleDefinition rDefination = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                    SPRoleAssignment rAssignment = new SPRoleAssignment(group);
                    //adding role definition to role assignment
                    rAssignment.RoleDefinitionBindings.Add(rDefination);
                    web.RoleAssignments.Add(rAssignment);
                    web.Update();
                }
            }



How add roles to existing groups:

In this example , showing how to assign roles to existing groups.

            using (SPSite site = new SPSite("http://UKREDDY:6969/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPGroup group = web.SiteGroups["TestGroup"];
                    //Getting Pre-Defined Roles
                    SPRoleDefinition rDefination = web.RoleDefinitions.GetByType(SPRoleType.WebDesigner);
                    SPRoleAssignment rAssignment = new SPRoleAssignment(group);
                    //adding role definition to role assignment
                    rAssignment.RoleDefinitionBindings.Add(rDefination);
                    web.RoleAssignments.Add(rAssignment);
                    web.Update();
                }
            }

How  to delete a role from the group:

In this example, wanna show how to remove a role from the group.


            using (SPSite site = new SPSite("http://UKREDDY:6969/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPGroup group = web.SiteGroups["TestGroup"];
                    SPRoleDefinition roleDef = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                    SPRoleAssignment roleAss = web.RoleAssignments.GetAssignmentByPrincipal((SPPrincipal)group);
                    roleAss.RoleDefinitionBindings.Remove(roleDef);
                    roleAss.Update();
                }
            }


How to delete all roles to a group:

In this example, i wanna show how to remove all roles from a group.

            using (SPSite site = new SPSite("http://UKREDDY:6969/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPGroup group = web.SiteGroups["TestGroup"];
                    web.RoleAssignments.Remove(group);
                    web.Update();
                }
            }





No comments:

Post a Comment