How to avoid Access denied page in sharepoint

Description

Access denied exception is handled by sharepoint platform and user will be redirected to _layouts/AccessDenied.aspx page if user doesn’t have permission to perform that task. This might cause usability problem in some cases

The Code:

You can handle access denied exception in your code by setting CatchAccessDeniedException value to true.
Following code snippet shows how to handle access denied exception:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool catchException = SPSecurity.CatchAccessDeniedException;
SPSecurity.CatchAccessDeniedException = false;
try
{
	//updating list item
	SPList list = SPcontext.Current.Web.List["TestList"];
	SPListItem item = list.Items[0];
	item["title"] = “Some value;
	//If user doesnt have permission, exception will be thrown
	//If value of CatchAccessDeniedException is true, then user will be
	//redirected to AccessDenied.aspx page
	item.Update();
}
catch(Exception ex)
{
	//Your custom error message can be shown here
}
finally
{
	//reset the flag to original value
	SPSecurity.CatchAccessDeniedException = catchException;
}


Summary

As you can see this solution is pretty straight forward. Only you need to aware about SPSecurity.CatchAccessDeniedException property.
Hope it helps!

calendarFebruary 22, 2012 · cardInfoyen · commentsNo Comments
tagTags: , , ,  · Posted in: MOSS, SharePoint, SharePoint 2010

Leave a Reply

Spam Protection: , required

myworldmaps infoyen