Partilhar via


SPList.DoesUserHavePermissions Method (SPUser, SPBasePermissions)

Indicates whether the specified user has the specified set of permissions on the list.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Function DoesUserHavePermissions ( _
    user As SPUser, _
    permissionMask As SPBasePermissions _
) As Boolean
'Usage
Dim instance As SPList
Dim user As SPUser
Dim permissionMask As SPBasePermissions
Dim returnValue As Boolean

returnValue = instance.DoesUserHavePermissions(user, _
    permissionMask)
public bool DoesUserHavePermissions(
    SPUser user,
    SPBasePermissions permissionMask
)

Parameters

Return Value

Type: System.Boolean
true if the user has the permissions; otherwise, false.

Exceptions

Exception Condition
UnauthorizedAccessException

The current user does not have the EnumeratePermissions right.

Examples

The following example is a console application that enumerates all the users in all the groups of a Web site, checking whether each user has a particular set of permissions on the Shared Documents list.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("https://localhost")
         Using web As SPWeb = site.RootWeb

            Dim perms As SPBasePermissions = SPBasePermissions.AddListItems _
                                          Or SPBasePermissions.ApproveItems _
                                          Or SPBasePermissions.CancelCheckout _
                                          Or SPBasePermissions.DeleteListItems _
                                          Or SPBasePermissions.DeleteVersions _
                                          Or SPBasePermissions.EditListItems _
                                          Or SPBasePermissions.Open _
                                          Or SPBasePermissions.OpenItems _
                                          Or SPBasePermissions.ViewListItems _
                                          Or SPBasePermissions.ViewVersions

            Dim listUrl As String = web.RootFolder.ServerRelativeUrl + "shared documents"
            Dim list As SPList = web.GetList(listUrl)

            ' Be sure the current user has permission to check permissions.
            If web.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions) Then

               For Each group As SPGroup In web.Groups
                  For Each user As SPUser In group.Users
                     ' Check another user's permissions.
                     Dim hasPermission As Boolean = list.DoesUserHavePermissions(user, perms)
                     Console.WriteLine("{0, -5}  {1}", hasPermission, user.LoginName)
                  Next
               Next

            End If

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub
End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("https://localhost"))
         {
            using (SPWeb web = site.RootWeb)
            {
               SPBasePermissions perms = SPBasePermissions.AddListItems
                                       | SPBasePermissions.ApproveItems
                                       | SPBasePermissions.CancelCheckout
                                       | SPBasePermissions.DeleteListItems
                                       | SPBasePermissions.DeleteVersions
                                       | SPBasePermissions.EditListItems
                                       | SPBasePermissions.Open
                                       | SPBasePermissions.OpenItems
                                       | SPBasePermissions.ViewListItems
                                       | SPBasePermissions.ViewVersions;


               string listUrl = web.RootFolder.ServerRelativeUrl + "shared documents";
               SPList list = web.GetList(listUrl);

               // Be sure the current user has permission to check permissions.
               if (web.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions))
               {
                  foreach (SPGroup group in web.Groups)
                  {
                     foreach (SPUser user in group.Users)
                     {
                        // Check another user's permissions.
                        bool hasPermission = list.DoesUserHavePermissions(user, perms);
                        Console.WriteLine("{0, -5}  {1}", hasPermission, user.LoginName);
                     }
                  }
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}

See Also

Reference

SPList Class

SPList Members

DoesUserHavePermissions Overload

Microsoft.SharePoint Namespace