SPList.GetUserEffectivePermissions method

Gets the effective permissions that a specified user has on the list.

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

Syntax

'Declaration
Public Overrides Function GetUserEffectivePermissions ( _
    userName As String _
) As SPBasePermissions
'Usage
Dim instance As SPList
Dim userName As String
Dim returnValue As SPBasePermissions

returnValue = instance.GetUserEffectivePermissions(userName)
public override SPBasePermissions GetUserEffectivePermissions(
    string userName
)

Parameters

  • userName
    Type: System.String

    The name the user whose permissions are to be returned.

Return value

Type: Microsoft.SharePoint.SPBasePermissions
A bitwise combination of enumeration values that specifies a set of permissions.

Exceptions

Exception Condition
UnauthorizedAccessException

The current user does not have the permission to enumerate permissions.

Examples

The following example is a console application that enumerates all the users in all the groups of a website, checking whether each user has the ApproveItems permission on the Shared Documents library. For an example showing another approach to the same task, see the DoesUserHavePermissions(SPUser, SPBasePermissions) method.

Imports System
Imports Microsoft.SharePoint

Module Test

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

                ' Get a list to check permissions on.
                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

                            ' Get the rights mask for a user.
                            Dim permMask As SPBasePermissions = list.GetUserEffectivePermissions(user.LoginName)

                            ' Check if the user has a specific right.
                            Dim hasPermission As Boolean = (permMask & SPBasePermissions.ApproveItems) <> 0
                            Console.WriteLine("{0} {1} permission to approve items.", _
                                              user.LoginName, IIf(hasPermission, "has", "does not have"))
                        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 Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // Get a list to check permissions on.
                    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)
                            {
                                // Get the rights mask for a user.
                                SPBasePermissions permMask = list.GetUserEffectivePermissions(user.LoginName);

                                // Check if the user has a specific right.
                                bool hasPermission = (permMask & SPBasePermissions.ApproveItems) != 0;
                                Console.WriteLine("{0} {1} permission to approve items.", 
                                                  user.LoginName, hasPermission ? "has" : "does not have");
                            }
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}

See also

Reference

SPList class

SPList members

Microsoft.SharePoint namespace

EffectiveBasePermissions

GetUserEffectivePermissionInfo(String)