2011-04-07 82 views
3

我想遍历网站集中的所有网站并更改某个网站的权限。在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(); 
     } 

,但它仍然抛出了同样的错误。

回答

0

我不确定是什么问题。 但它可能发生该DLL不被更新。它发生在我身上好几次了。 尝试从x:/ windows/assembly中删除DLL,然后重新部署解决方案以查看行为是否有任何更改。 也可以尝试在此之前提交修改(我敢肯定,你已经尝试过了)

问候,

佩德罗

1

我会尽力做一个SPWeb.Update然后

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

此外,不要忘记在foreach循环中打开的所有AllWebs上执行SPWeb.Dispose

1

如果您在功能中使用此代码,请确保您使用Web级别的功能。不要通过站点级功能中的AllWebs循环。

更改功能范围,以Web和尝试下面的代码

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; 
} 

在激活所需的网站这个功能应用的权限。

相关问题