Home > MOSS > Check if User Has Permissions on List or WebSite

Check if User Has Permissions on List or WebSite

June 11th, 2010

Recently I was working on a web part where I have to check for current user permissions for different lists in multiple sub-sites. I found a very straight forward method from SharePoint API. (DoesUserHavePermissions)

This method has some variations on site and list levels. 

On site level this method can take two parameters, string loginname and SPBasePermissions. You can use these parameters with two variations. If you don’t pass the login name then it verifies the permission level against the current user. SPBasePermissions is an enumeration. You can find more details about SPBasePermissions here.

On list level this method can again take two parameters, but three variations in method parameters. If you just pass the SPBasePermissions then it verifies the permission level against the current user. Then you can check permissions for a specific user and in third variation you can apply folder check. You can find more details on SPList.DoesUserHavePermissions here

You can use this method like this

if (oWeb.DoesUserHavePermissions(SPBasePermissions.ViewPages))
{
       SPList oList = oWeb.Lists[ListName];

        if (oList.DoesUserHavePermissions(SPBasePermissions.ViewListItems))
        {
         // some smart code
        }
}

Thanks

  1. Cheng
    August 24th, 2010 at 04:29 | #1

    Yo thanks for the codes.
    But the problem is that. IF the user has no rights/permission in viewing the list,
    they couldn’t even retrieve the list before performing oList.DoesUserHavePermissions();
    olist In this case is a NUll to them.
    What i’m trying to say is that. the List is Hidden to them if the user has no permission on it.

  2. admin
    September 17th, 2010 at 00:55 | #2

    Yes you are right in this case you can use the same method like this
    SPUser user = SPContext.Current.Web.CurrentUser;
    /* Function to check if user has view permissions for site */
    if (oWeb.DoesUserHavePermissions(user.LoginName, SPBasePermissions.ViewPages))
    {
    SPList oList = oWeb.Lists[ListName];
    /* Function to check if user has permissions for list */
    if (oList.DoesUserHavePermissions(user, SPBasePermissions.ViewListItems))
    {
    result = true;
    }
    }

    you probably need to use RunWithElevatedPermission to use this code.

    Please let me know if you still have problem using this.

    thanks

  3. September 1st, 2011 at 06:29 | #3

    I liked your article is an interesting technology
    thanks to google I found you

Comments are closed.