Thursday, January 20, 2011

Assign User Group as owners to Sharepoint site

Here is the logic to assign a User Group as owners to a site.

using (SPWeb web = site.OpenWeb("MySite"))
{
    SPWeb parentWeb = web.ParentWeb;
    SPGroup ownersGroup = parentWeb.SiteGroups["SomeOwnersGroup"];
    web.AssociatedOwnerGroup = ownersGroup;

    web.BreakRoleInheritance(true);

    SPRoleAssignment roleassignment = new SPRoleAssignment(ownersGroup);
    SPRoleDefinition roleDefinition = parentWeb.RoleDefinitions["Full Control"];
    roleassignment.RoleDefinitionBindings.Add(roleDefinition);
    
    web.RoleAssignments.Add(roleassignment);

    web.Update();
}

Hope this helps.

No comments:

Post a Comment