Thursday, January 12, 2006

 

Sharepoint Portal Object Model --Tips and Tricks

Catching Access Denied Exception

Anytime you find yourself working with the sharepoint object ,you might jump into a requirement ,where you need to catch the Access Denied Exception and perform your own structured exception handling rather than resorting to the one provided by Sharepoint.
Consider this piece of code


try
{
//Impersonate the administrator;
SPSite site=new SPSite(“http://portal-name”);

//******************************/
site.CatchAccessDeniedException=true;
//******************************/

SPWeb web=site.OpenWeb(“/sites/AdminSite”);
//revert back to the current user (in this case reader);

//this line of code throws the access denied exception
web.Permissions.DoesUserHavePermission(SPRights.ViewWeb);
}
catch(AccessDeniedException exp)
{
//Do you own handling
}


In case you don’t set the AccessDenied flag,SPS will abort the current and redirect the user to the “Unauthorized Page”.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?