我想遍历网站集中的所有网站并更改某个网站的权限。在SharePoint 2010中以编程方式更改组的权限级别?
我的代码是:
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);
}
}
}
,但它给了我一个错误
Error changing permissions, details: There are uncommitted changes on the SPWeb object, call SPWeb.Update() to commit the changes before calling this method.
如果我想要做同样的代码工作得很好,但对列表,而不是
SPListCollection collList = web.Lists;
foreach (SPList oList in collList)
{
//and so on
我试图把web.Update()放在不同的地方,但没有成功。有任何想法吗?
在此先感谢。
编辑:
我注释掉了大部分的东西,只剩
if (web.Name == "SiteName")
{
web.BreakRoleInheritance(true);
web.Update();
}
,但它仍然抛出了同样的错误。