SPPermissionCollection.DoesUserHavePermissions Method

NOTE: This API is now obsolete.

Returns a value that indicates whether the current user has the specified permissions.

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

Syntax

'Declaration
<ObsoleteAttribute("Use the SPRoleAssignmentCollection class instead")> _
Public Function DoesUserHavePermissions ( _
    permissionMask As SPRights _
) As Boolean
'Usage
Dim instance As SPPermissionCollection
Dim permissionMask As SPRights
Dim returnValue As Boolean

returnValue = instance.DoesUserHavePermissions(permissionMask)
[ObsoleteAttribute("Use the SPRoleAssignmentCollection class instead")]
public bool DoesUserHavePermissions(
    SPRights permissionMask
)

Parameters

Return Value

Type: System.Boolean
true if the current user has the specified permissions; otherwise, raises an access-denied exception and prompts the user for authentication.

Remarks

To prevent the user from being prompted for authentication by the DoesUserHavePermissions method, you can set the CatchAccessDeniedException property of the SPSite or the SPVirtualServer class to false and use error handling to capture the exception.

Examples

The following code example iterates through all lists of a site and uses the DoesUserHavePermissions method to specify lists for which the current user has permissions.

Dim siteCollection As New SPSite("http://Server_Name")
Dim site As SPWeb = siteCollection.OpenWeb()

siteCollection.CatchAccessDeniedException = False

Dim lists As SPListCollection = site.Lists
lists.ListsForCurrentUser = True
Dim listCount As Integer = lists.Count

Dim i As Integer

For i = 0 To listCount - 1
    Response.Write(("<br>" + lists(i).Title.ToString() + " : "))

    Try
        If lists(i).Permissions.DoesUserHavePermissions
                (SPRights.ViewListItems) Then
            Response.Write("Has Permissions")
        End If
    Catch ex As System.UnauthorizedAccessException
        Response.Write(ex.Message.ToString())
    End Try

Next i
using(SPSite oSiteCollection = new SPSite("http://Server_Name"))
{
    SPWeb oWebsite = siteCollection.OpenWeb();
    oSiteCollection.CatchAccessDeniedException = false;

    SPListCollection collLists = oWebsite.Lists;
    collLists.ListsForCurrentUser = true;
    int intListCount = collLists.Count;

    for(int intIndex=0; intIndex<intListCount; intIndex++)
    {
        Response.Write("<br>" + colLLists[intIndex].Title.ToString() 
            + " : ");
        try 
        {
            if(collLists[intIndex].Permissions.DoesUserHavePermissions
                (SPRights.ViewListItems))
            {
                Response.Write("Has Permissions");
            }
        }

        catch(System.UnauthorizedAccessException ex) 
        {
            Response.Write(ex.Message.ToString());
        }
    }
}

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

See Also

Reference

SPPermissionCollection Class

SPPermissionCollection Members

Microsoft.SharePoint Namespace