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

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

share|improve this question
add comment

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:

site.RootWeb.AssociatedOwnerGroup.Update();
share|improve this answer
    
in this case, the get accessor of SPSite.Web property caches the result... –  Steve B Jun 30 '11 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 '11 at 12:36
    
Did running the Update() method of the AssociatedOwnerGroup work for you? –  Tim Gabrhel Jun 30 '11 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 '11 at 13:52
add comment

Your Answer

 
discard

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

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