SharePoint 2010 Check User Permission on Web

In this article I would like to explore multiple ways to check User Permissions on a web object.

Requirement

We need to check whether a particular user has open permission on a web object.

Solution

We can use the web.DoesUserHavePermissions() method for this purpose.

Share1.jpg

The method has 2 overloads, but we are using the second one that takes login as argument. The second argument SPBasePermissions can be used to specify the type of permission.

Share2.jpg

Implementation

For testing purposes create a console application, add a reference to SharePoint DLLs & change the following project properties:

  1. Target Framework to .Net 3.5
  2. Platform Target to Any CPU

Place the following code in the main method:

using (SPSite site = new SPSite("http://localhost"))

{

    using (SPWeb web = site.OpenWeb())

    {

        bool result = web.DoesUserHavePermissions("DOM\USR", SPBasePermissions.Open);

 

        Console.WriteLine(result);

    }

}

 

Console.ReadKey(false);

Please use the appropriate user names in the code.

Execution

For the preceding site, assign a user with Read Permission. Execute code & you will get the result TRUE.

Now, remove the user from all permissions, including Authenticated users. You will get the result FALSE.

Note: Although the preceding method was invoked using SPWeb object, we can invoke it from SPList and SPItem as well. In general the method is available for all SPSecurableObject types.

  1. SPWeb inherits from SPSecurableObject

  2. SPList inherits from SPSecurableObject

  3. SPListItem inherits from SPSecurableObject

References

http://bit.ly/11vfDCq

Summary

In this article we have explored how to check User Permissions on a particular web object. In real-world scenarios we need the method for scenarios like custom branding, custom events etcetera.