A Developer's Diary

Dec 22, 2012

Sharepoint 2010 - Access denied Exception

The below code retrieves sharepoint SPList item when executed through a .net console application. The same piece of code when run through ASP.NET application throws Access is denied exception.

SPWeb web = GetWeb(SiteURL);
return web.Lists[ListName];
The exception message through the visual studio watch window
{"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"} System.Exception {System.UnauthorizedAccessException}
To fix this problem, you need to run the above snippet with elevated privileges using SPSecurity.RunWithElevatedPrivileges
SPList list = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPWeb web = GetWeb(SiteURL);
    list = web.Lists[ListName];
});

return list;

No comments :

Post a Comment