Cannot catch the SharePoint Access denied error
Asked Answered
G

2

7

I am trying to access a sharepoint list programmatically in a webpart, like this.

try
{
      masterList = web.Lists[listId];
}
catch(Exception e)
{
      RenderExceptionMessage(e.Message);
}

The RenderExceptionMessage() method is supposed to show a user-friendly error message inside the webpart.

But the problem is that I am not able to trap the Exception. Instead the webpart page redirects to an access denied page which shows an error message "You are currently signed in as: Domain\user"

Also, the message of the exception being caught reads "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."

Any idea why this behaves this way?

Grizelda answered 7/4, 2009 at 8:43 Comment(0)
L
9

By default, SharePoint has custom handling for access denied exceptions (including the redirect to the custom page) within page/webservice requests, bypassing the excepton handling in your code.

To disable this custom handling, set SPSecurity.CatchAccessDeniedException to false.

Lafayette answered 7/4, 2009 at 10:33 Comment(0)
E
1

Maybe another way to handle this is to add some defensive programming such as a check to make sure the user has access to the SPWeb and/or SPList. Off the top of my head I think that SPWeb.EnsureUser can help. SPList.CheckPermissions or SPList.DoesUserHavePermissions may help as well.

Exerciser answered 7/4, 2009 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.