Quantcast
Channel: Periwinkle IT » SharePoint 2007
Viewing all articles
Browse latest Browse all 10

Running code with elevated permissions

$
0
0

Recently, I had to allow a reader to be able to add to a list, and an image library, programatically.

After some hair pulling, this appears to do the trick!

SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();

SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Get references to the site collection and site for the current context.
// The using statement makes sures these references are disposed properly.
using (SPSite siteCollection = new SPSite(mySite.ID))
{
using (SPWeb web = siteCollection.OpenWeb(myWeb.ID))
{
web.AllowUnsafeUpdates = true;

try
{
//Code goes here
}
catch (Exception ex)
{
//Error handling goes here
}
finally
{
if (web != null)
{
web.AllowUnsafeUpdates = false;
}
}
}
}
});


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images