Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

We have a bucketload of team sites that are created using custom code and these are constantly being created/updated/deleted.

Our security requirements mean that these sites must have unique permissions with associated owner/member/visitor groups.

When the site is deleted, however, the associated groups are left hanging around and have to be deleted manually. This doesn't happen if we create the team site through the normal sharepoint web interface.

Is there any way to make it so that our custom created groups are deleted when the site is?

We're using the following code to add the groups:

                //Create the owners group
                newSite.SiteGroups.Add(ownersName, owner, owner, siteName + " Owners Group");
                SPGroup ownersGroup = newSite.SiteGroups[ownersName];
                ownersGroup.AllowMembersEditMembership = true;
                newSite.AssociatedOwnerGroup = ownersGroup;

                //Add RoleAssignments and RoleDefinitions
                SPRoleAssignment ownerAssignment = new SPRoleAssignment(ownersGroup);
                SPRoleDefinition ownerDefinition = newSite.RoleDefinitions["Full Control"];
                ownerAssignment.RoleDefinitionBindings.Add(ownerDefinition);
                newSite.RoleAssignments.Add(ownerAssignment);
                ownersGroup.Update();
share|improve this question
add comment

1 Answer 1

I'm assuming that these are subsites?

You'll need to create an event listener on the Site Collection, watching for web deletion events. At that point, you'll be able to delete the appropriate groups using the context object the listener handler gives you.

share|improve this answer
    
Thanks. That seems a bit roundabout though. How come the standard sharepoint team sites get their groups deleted by default? –  Anonymous Apr 30 '10 at 7:27
    
Open up the code for the deleteweb.aspx page, and see what Microsoft is doing - you may be surprised how complex some of the underlying code is (though maybe not this - I haven't looked). –  Greg Hurlman Apr 30 '10 at 12:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.