Friday, September 5, 2008

UnauthorizedAccessException and SPSecurity.RunWithElevatedPrivileges to the rescue

This issue occured when a user with read-only access would access a page with a web part that did some Excel parsing.

The code would throw an UnauthorizedAccessException error when attempting to instantiate the Microsoft.Office.Interop.Excel.Application() COM object, . To get around this in MOSS we can elevate the permissions to that of the SharePoint system account by wrapping that code as follows:

SPSecurity.RunWithElevatedPrivileges(
delegate()
{
// Put the code that needs to run with elevated permissions here
BashExcelSpreadsheet();
}
);

We love you SPSecurity

1 comment:

Anonymous said...

If you would like to obtain a good deal from this
post then you have to apply such techniques to your won weblog.


Look at my web blog; attu