Partager via


SPPermissionCollection Class

NOTE: This API is now obsolete.

Use the new SPRoleDefinition and SPRoleAssignment classes instead, to define roles and to assign users to them. For more information, see Changes in the Authorization Object Model. (In Windows SharePoint Services 2.0, SPRole represented a collection of SPPermission objects and is maintained for backward compatibility.)

Inheritance Hierarchy

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

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

<ObsoleteAttribute("Use the SPRoleAssignmentCollection class instead")> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class SPPermissionCollection _
    Inherits SPBaseCollection

Dim instance As SPPermissionCollection
[ObsoleteAttribute("Use the SPRoleAssignmentCollection class instead")]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class SPPermissionCollection : SPBaseCollection

Remarks

Use the Permissions property of either the SPList or SPWeb class to return the collection of permissions for a list or a site. To create a permission, use either the Add or AddCollection method of SPPermissionCollection.

Use an indexer to return a single permission from the collection. For example, if the collection is assigned to a variable named collPermissions, use collPermissions[index] in C#, or collPermissions(index) in Visual Basic, where index is either the index number of the permission in the collection or the SPMember object for a user or group that has the permission for the list or site.

Examples

The following code example uses the SPPermissionCollection class to display the name and permissions for each user who has access to a specified list. The example iterates through all the users of a site and through all the permissions for a list; if a user ID matches the member ID of a list permission, information for the user is displayed.

This example requires using directives (Imports in Visual Basic) for the [Microsoft.SharePoint] and [Microsoft.SharePoint.Utilities] namespaces.

Dim siteCollection As SPSite = SPContext.Current.Site
Dim site As SPWeb = siteCollection.AllWebs("Site_Name")
Dim list As SPList = site.Lists("List_Name")
Dim perms As SPPermissionCollection = list.Permissions
Dim users As SPUserCollection = site.Users

Dim user As SPUser

For Each user In  users

    Dim perm As SPPermission

    For Each perm In  perms

        If user.ID = perm.Member.ID Then

            Response.Write("User: " & SPEncode.HtmlEncode(user.Name) _
                & " Permissions: " & perm.PermissionMask.ToString() 
                & "<BR>")

        End If

    Next perm

Next user
SPSite oSiteCollection = SPContext.Current.Site;
using(SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"])
{
    SPList oList = oWebsite.Lists["List_Name"];
    SPPermissionCollection collPermissions = oList.Permissions;
    SPUserCollection collUsers = oWebsite.Users;

    foreach (SPUser oUser in collUsers)
    {

        foreach (SPPermission oPermission in collPermissions)
        {

            if (oUser.ID == oPermission.Member.ID)
            {
                 Response.Write("User: " + 
                    SPEncode.HtmlEncode(oUser.Name) + 
                    " Permissions: " +   
                    oPermission.PermissionMask.ToString() + 
                    "<BR>");
            }
        }
    }
}

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPPermissionCollection Members

Microsoft.SharePoint Namespace