DoesUserHavePermissions

on August 22, 2006

Here is the best way to evaluate if a user can view a list. I had many problems with a web part and after investigations, this method resolve the problems.

Read the comments in the code to learn more about it!

Code:

public bool UserCanViewListItems(SPWeb web, SPList list)
{
bool res = true;
SPWeb myweb = null;
SPSite mysite = null;
try
{
/* There is a kind of ownership problem related to the DoesUserHavePermissions.
* To fix that problem, the user performing the execution of the method, needs to
* be the same as the one who created the Parent Object. For this reason, This
* Method use the List object received in param and recreate the Parent object to
* get the ownership. And it works perfectly.*/
mysite = new SPSite(list.ParentWeb.Url);
myweb = mysite.OpenWeb();
list = myweb.Lists[list.Title];
list.Permissions.DoesUserHavePermissions(SPRights.ViewListItems);
}
catch(Exception)
{
res = false;
}
finally
{
if(myweb!=null)
myweb.Dispose();
if(mysite!=null)
mysite.Dispose();
}
return res;
}

0 comments:

ShareThis