Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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)
public bool DoesUserHavePermissions(
	SPUser user,
	SPBasePermissions permissionMask
)

Parameters

user
Type: Microsoft.SharePoint.SPUser

A user for which to verify permissions.

permissionMask
Type: Microsoft.SharePoint.SPBasePermissions

A bitwise combination of enumeration values that specifies a set of permissions.

Return value

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

The current user does not have the EnumeratePermissions right.

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.

using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("http://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();
      }
   }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.