In this article I would like to explore multiple ways to check User Permissions on a web object.
We need to check whether a particular user have Open permission on a web object.
We can use the web.DoesUserHavePermissions() method for this purpose.
The method has 2 overloads, but we are using the second one which takes login as argument. The second argument SPBasePermissions can be used to specify type of permission.
For testing purpose create a console application, add reference to SharePoint DLLS & change 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);
Please use the appropriate user names in the code.
For the above 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.
Although the above method was invoked using SPWeb object, we can invoke it from SPList, SPItem as well. In general the method is available for all SPSecurableObject type.
1. SPWeb inherits from SPSecurableObject
2. SPList inherits from SPSecurableObject
3. SPListItem inherits from SPSecurableObject
In this article we have explored how to check User Permissions on a particular web object. In real world scenario we need the method for scenarios like custom branding, custom event etc.v