Condividi tramite


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)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Function DoesUserHavePermissions ( _
    login As String, _
    permissionMask As SPBasePermissions _
) As Boolean
'Usage
Dim instance As SPWeb
Dim login As String
Dim permissionMask As SPBasePermissions
Dim returnValue As Boolean

returnValue = instance.DoesUserHavePermissions(login, _
    permissionMask)
public bool DoesUserHavePermissions(
    string login,
    SPBasePermissions permissionMask
)

Parameters

Return Value

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

Remarks

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.

Examples

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.

Imports System
Imports Microsoft.SharePoint

Module Test

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

                ' Make sure the current user can enumerate permissions.
                If web.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions) Then
                    ' Specify the permission to check.
                    Dim permissionToCheck As SPBasePermissions = SPBasePermissions.ManageLists
                    Console.WriteLine("The following users have {0} permission:", permissionToCheck)

                    ' Check the permissions of users who are explicitly assigned permissions.
                    Dim users As SPUserCollection = web.Users
                    Dim user As SPUser
                    For Each user In users
                        Dim login As String = user.LoginName
                        If web.DoesUserHavePermissions(login, permissionToCheck) Then
                            Console.WriteLine(login)
                        End If
                    Next
                End If

            End Using
        End Using
        Console.ReadLine()
    End Sub

End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://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();
        }
    }
}

See Also

Reference

SPWeb Class

SPWeb Members

DoesUserHavePermissions Overload

Microsoft.SharePoint Namespace

EnsureUser(String)