SPRoleDefinitionCollection 类

表示定义可用于在 Web 站点中的角色定义的SPRoleDefinition对象的集合。

继承层次结构

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.SPRoleDefinitionCollection

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public NotInheritable Class SPRoleDefinitionCollection _
    Inherits SPBaseCollection
用法
Dim instance As SPRoleDefinitionCollection
public sealed class SPRoleDefinitionCollection : SPBaseCollection

备注

角色定义可以继承父SPWeb对象或本地定义。为了具有独特角色定义,您必须具有独有权限 (角色分配),但独有权限可以有任何一个唯一或继承的角色定义。

利用SPWeb类的RoleDefinitions属性可以为 Web 站点的角色定义的集合。若要创建一个角色定义,使用SPRoleDefinition类的构造函数来实例化的对象上的对象,设置属性,然后调用**Add()**方法可向集合中添加新的角色定义。

使用索引器从集合中返回一个项。例如,如果该集合分配给名为collRoleDefinitions的变量中,使用在 C# collRoleDefinitions[index]或collRoleDefinitions(index)在 Visual Basic,其中index是集合中的项的索引号,或者包含该角色定义的名称的字符串。

没有一种方法来检索用户角色分配SharePoint Foundation列表对象上。但是,下面的代码使您可以检索此列表。

private void AddListRoleAssignmentNodes(SPList objList)
{ 
   try
   {
      if (objList.HasUniqueRoleAssignments)
      {
         SPRoleAssignmentCollection oRoleAssignments =
            objList.RoleAssignments;

         foreach (SPRoleAssignment oRoleAssignment in oRoleAssignments)
         {
            SPPrincipal oPrincipal = oRoleAssignment.Member;
            try
            {
               // Retrieve users having explicit permissions on the list
               SPUser oRoleUser = (SPUser)oPrincipal;
            }
            catch (Exception ex)
            {
               string msg = ex.Message;
            }
            try
            {
               // Retrieve user groups having permissions on the list
               SPGroup oRoleGroup = (SPGroup)oPrincipal;

               if (oRoleGroup.Users.Count > 0)
               { 
                  string strGroupName = oRoleGroup.Name;
                  // Add code here to retrieve Users inside this User-Group
               }
            }
            catch (Exception ex)
            {
               string msg = ex.Message;
            }
         }
      }
   }
   catch (Exception ex)
   {
      string msg = ex.Message;
   }
}
Private Sub AddListRoleAssignmentNodes(ByVal objList As SPList)
   Try
     If objList.HasUniqueRoleAssignments Then
       Dim oRoleAssignments As SPRoleAssignmentCollection = objList.RoleAssignments

       For Each oRoleAssignment As SPRoleAssignment In oRoleAssignments
         Dim oPrincipal As SPPrincipal = oRoleAssignment.Member
         Try
            ' Retrieve users having explicit permissions on the list
            Dim oRoleUser As SPUser = CType(oPrincipal, SPUser)
         Catch ex As Exception
            Dim msg As String = ex.Message
         End Try
         Try
            ' Retrieve user groups having permissions on the list
            Dim oRoleGroup As SPGroup = CType(oPrincipal, SPGroup)

            If oRoleGroup.Users.Count > 0 Then
              Dim strGroupName As String = oRoleGroup.Name
              ' Add code here to retrieve Users inside this User-Group
            End If
         Catch ex As Exception
            Dim msg As String = ex.Message
         End Try
       Next oRoleAssignment
     End If
   Catch ex As Exception
     Dim msg As String = ex.Message
   End Try
End Sub

示例

后将换角色定义继承,下面的代码示例创建角色定义,其中包括所有权利,除ManagePermissions;此外设置属性值,并将新的角色定义添加到站点的角色定义的集合。

string strRoleName = "Custom Role Definition";
using (SPSite oSiteCollection = new SPSite("http://Server_Name/Subsite1"))
{
    using (SPWeb oWebsite = oSiteCollection.OpenWeb())
    {

        if (!oWebsite.HasUniqueRoleDefinitions)
        {
            oWebsite.RoleDefinitions.BreakInheritance(true, true);
        }

        SPRoleDefinition oRoleDefinition = new SPRoleDefinition();
        oRoleDefinition.Name = strRoleName;
        oRoleDefinition.Description = "A role definition with all rights except ManagePermissions";
        oRoleDefinition.BasePermissions = SPBasePermissions.FullMask ^ SPBasePermissions.ManagePermissions;
        oWebsite.RoleDefinitions.Add(oRoleDefinition);
    }
}
Dim strRoleName As String = "Custom Role Definition"
Using oSiteCollection As New SPSite("http://Server_Name/Subsite1")
   Using oWebsite As SPWeb = oSiteCollection.OpenWeb()

      If Not oWebsite.HasUniqueRoleDefinitions Then
         oWebsite.RoleDefinitions.BreakInheritance(True, True)
      End If

      Dim oRoleDefinition As New SPRoleDefinition()
      oRoleDefinition.Name = strRoleName
      oRoleDefinition.Description = "A role definition with all rights except ManagePermissions"
      oRoleDefinition.BasePermissions = SPBasePermissions.FullMask Xor SPBasePermissions.ManagePermissions
      oWebsite.RoleDefinitions.Add(oRoleDefinition)
   End Using
End Using

备注

某些对象实现IDisposable接口,并必须避免后不再需要保留这些对象在内存中。好的编码做法有关的信息,请参阅Disposing Objects

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPRoleDefinitionCollection 成员

Microsoft.SharePoint 命名空间

RoleDefinitions