In a feature receiver, I want to create group and set the associated owner group of the Web. This feature is activated automatically from a custom site template (I think this the root of the problem).
The code does not throw error, but after the creation of the site the groups are correctly created (with its members), but the SPWeb.AssociatedOwnerGroup is not set.
What can explain that ? I suspect the site template creation process set up this groups after enabling the features... but I don't have a way to verify.
here is my code :
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var web = properties.Feature.Parent as SPWeb;
var site = properties.Feature.Parent as SPSite;
if (web == null)
{
web = site.RootWeb;
}
else if (site == null)
{
site = web.Site;
}
site.RootWeb.AssociatedOwnerGroup = EnsureGroup(site, "Administrateurs", "Contrôle total"); // Full control in French
site.RootWeb.Update();
}
private static SPGroup EnsureGroup(SPSite site, string groupName, string roleDefinitionName)
{
var result = site.RootWeb.SiteGroups.Cast<SPGroup>().SingleOrDefault(g => g.Name == groupName);
if (result == null)
{
site.RootWeb.SiteGroups.Add(
groupName,
site.Owner,
site.Owner,
"");
if(!string.IsNullOrEmpty(roleDefinitionName))
{
result = site.RootWeb.SiteGroups[groupName];
var role = new SPRoleAssignment(result);
role.RoleDefinitionBindings.Add(
site.RootWeb.RoleDefinitions[roleDefinitionName]
);
site.RootWeb.RoleAssignments.Add(role);
site.RootWeb.Update();
}
}
return result;
}
PS: I'm working on a SP 2010 farm