开发者

Change permission level for groups programmatically in SharePoint 2010?

I want to loop through all the sites in a site collection and change the permission for a certain site.

My code is:

        SPSite oSiteCollection = SPContext.Current.Site;
        SPWebCollection collWebsite = oSiteCollection.AllWebs;
        foreach (SPWeb web in collWebsite)
        {
            web.AllowUnsafeUpdates = true;

            if (web.Name == "SiteName")
            {
                web.BreakRoleInheritance(true);

                string[] groups = new string[2] { "Group1", "Group2" };

                foreach (string item in groups)
                {
                    SPGroup removeGroup = web.SiteGroups[item];
                    web.RoleAssignments.Remove(removeGroup);

                    SPGroup addGroup = web.SiteGroups[item];
                    SPRoleDefinition roleDefinition = web.RoleDefinitions["Read"];
                    SPRoleAssignment roleAssignment = new SPRoleAssignment(addGroup);
                    roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
                    web.RoleAssignments.Add(roleAssignment);
                }
            }

        }

but it gives me an error

Error changing permissions, details: There are uncommitted changes on the SPWeb object, call SPWeb.Update() to commit the changes before calling this method.

The code works just fine if I want to do the same but for lists instead

            SPListCollection collList = web.Lists;

            foreach (SPList oList in collList)
            {
                //and so on

I have tried to put web.Update() in various places but without success. Any ideas?

Thanks in advance.

Edit:

I commented out most of the stuff and only left

        if (web.Name == "SiteName")
        {
            web.BreakRoleInheritance(true);
 开发者_如何学Python           web.Update();
        }

but it still throws the same error.


I would try to do an SPWeb.Update and then

using(var newWeb = site.OpenWeb(web.ID))
{
    web.BreakRoleInheritance(true);
    web.Update();
}

Also, don't forget to do a SPWeb.Dispose on all the AllWebs opened in the foreach-loop.


If you are using this code in a Feature, then make sure that you use a Web level feature. Don't loop through AllWebs in a Site level feature.

Change the feature scope to Web and try the below code

using (SPWeb oWeb = SPContext.Current.Web)
{
    web.AllowUnsafeUpdates = true;
    web.BreakRoleInheritance(true);

    string[] groups = new string[2] { "Group1", "Group2" };

    foreach (string item in groups)
    {
        SPGroup removeGroup = web.SiteGroups[item];
        web.RoleAssignments.Remove(removeGroup);

        SPGroup addGroup = web.SiteGroups[item];
        SPRoleDefinition roleDefinition = web.RoleDefinitions["Read"];
        SPRoleAssignment roleAssignment = new SPRoleAssignment(addGroup);
        roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
        web.RoleAssignments.Add(roleAssignment);
    }
    web.AllowUnsafeUpdates = false;
}

Activate this feature in the required website to apply the permissions.


I'm not sure what the problem is. But it could happen that the DLL is not being updated. It happened to me several times. Try to delete the DLL from x:/windows/assembly and then redeploy the solution to see if there is any change on the behaviour. Also try to commit the changes before this (I'm sure you already tried it out)

Regards,

Pedro

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜