This topic has not yet been rated - Rate this topic

SPWeb.DoesUserHavePermissions method (String, SPBasePermissions)

Indicates whether the specified user has a specified set of permissions.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
public bool DoesUserHavePermissions(
	string login,
	SPBasePermissions permissionMask
)

Parameters

login
Type: System.String
The user name (Domain\User_Alias).
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 specified permissions; otherwise, false.

This method tests whether the permissions in permissionMask match those returned by GetUserEffectivePermissions(String).

The current user must have EnumeratePermissions rights to check the permissions of other users.

The following example is a console application that prints a list of users who have been explicitly assigned permission to manage the list on the current website.

using System;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // Make sure the current user can enumerate permissions.
                    if (web.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions))
                    {
                        // Specify the permission to check.
                        SPBasePermissions permissionToCheck = SPBasePermissions.ManageLists;
                        Console.WriteLine("The following users have {0} permission:", permissionToCheck);

                        // Check the permissions of users who are explicitly assigned permissions.
                        SPUserCollection users = web.Users;
                        foreach (SPUser user in users)
                        {
                            string login = user.LoginName;
                            if (web.DoesUserHavePermissions(login, permissionToCheck))
                            {
                                Console.WriteLine(login);
                            }
                        }
                    }
                }
            }
            Console.ReadLine();
        }
    }
}

Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.