I need to check whether a particular user has access to a site/subsite in SharePoint programmatically.
Note: login user can have acces to few sites in my SharePoint site. So I have to show sites only to which the user have permission.
I used:
using (SPWeb web = new SPSite(url).OpenWeb())
{
SPWebCollection Sites = web.Webs;
foreach (SPWeb website in Sites)
{
SPUser loginUser = website.CurrentUser;
string username = loginUser.Name;
if (!website.IsRootWeb)
{
SPWebCollection subsites = website.Webs;
foreach (SPWeb supersubsite in subsites)
{
SPWebCollection thirdlevelsites = supersubsite.Webs;
foreach (SPWeb thirdlevel in thirdlevelsites)
{
thirdlevel.Site.CatchAccessDeniedException = false;
bool check = thirdlevel.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open);
if (check)
{
}
thirdlevel.Site.CatchAccessDeniedException = true;
}
}
}
}
}
but getting error at
bool check = thirdlevel.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open);
as :
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))