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


    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)
                result = site.RootWeb.SiteGroups[groupName];

                var role = new SPRoleAssignment(result);

        return result;

PS: I'm working on a SP 2010 farm

1 Answer 1


I would try not calling site.RootWeb repeatedly like that. Sometimes in the SharePoint API calling a property like that (e.g. SPList.DefaultView ) actually returns a different object each time. Thus, your call to update is on a different object to the one you updated. I do wonder if that is happening here.

Instead, use a variable and pass that around:

SPWeb rootWeb = site.RootWeb;

That said, what I think you're missing is:

  • in this case, the get accessor of SPSite.Web property caches the result...
    – Steve B
    Jun 30, 2011 at 12:35
  • The code I provided, works if I run the "deploy" command from VS, but does not if the feature is activated from the site definition's onet.xml. I don't think this is related to the code itself (but I replaced all site.RootWeb, but a variable of kind SPWeb)
    – Steve B
    Jun 30, 2011 at 12:36
  • Did running the Update() method of the AssociatedOwnerGroup work for you? Jun 30, 2011 at 13:13
  • Ah, right, ONET.XML. Have you tried stapling it to your site definition, rather than activating it within your site definition. I find that that is more reliable. Also, what Tim said :)
    – Andy Burns
    Jun 30, 2011 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.