Last active
July 12, 2016 11:03
-
-
Save doeringp/f642ec214b4a34fc5b688174bc09fb18 to your computer and use it in GitHub Desktop.
Helper class to run code in SharePoint with elevated priviliges.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Doering.SharePoint; | |
class Program { | |
static void Main() { | |
// Access a list within the current web with higher privileges. | |
SPSecurityExtensions.RunInElevatedSite(delegate (SPSite site, SPWeb web) | |
{ | |
SPList list = web.Lists["MyList"]; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Enter file contents hereusing System; | |
using Microsoft.SharePoint; | |
using Microsoft.SharePoint.Utilities; | |
namespace Doering.SharePoint | |
{ | |
/// <summary> | |
/// Class with static helper methods to provide an easier access to SPSecurity.RunWithElevatedPrivileges(). | |
/// </summary> | |
public class SPSecurityExtensions | |
{ | |
public delegate void CodeToRunInElevatedEnvironment(SPSite elevatedSite, SPWeb elevatedWeb); | |
public static void RunInElevatedSite(CodeToRunInElevatedEnvironment secureCode, string webUrl, | |
bool allowUnsafeUpdates) | |
{ | |
SPSecurity.RunWithElevatedPrivileges(delegate | |
{ | |
SPSite site; | |
SPWeb web; | |
using (new SPMonitoredScope("Open elevated web and site")) | |
{ | |
site = new SPSite(webUrl); | |
try { web = site.OpenWeb(); } | |
catch | |
{ | |
site.Dispose(); | |
throw; | |
} | |
} | |
using (site) | |
using (web) | |
{ | |
try | |
{ | |
if (allowUnsafeUpdates) | |
{ | |
web.AllowUnsafeUpdates = true; | |
} | |
secureCode(site, web); | |
} | |
finally | |
{ | |
if (allowUnsafeUpdates) | |
{ | |
web.AllowUnsafeUpdates = false; | |
} | |
} | |
} | |
}); | |
} | |
public static void RunInElevatedSite(CodeToRunInElevatedEnvironment secureCode, string webUrl) | |
{ | |
RunInElevatedSite(secureCode, webUrl, false); | |
} | |
public static void RunInElevatedSite(CodeToRunInElevatedEnvironment secureCode, Guid siteId, Guid webId, | |
bool allowUnsafeUpdates) | |
{ | |
SPSecurity.RunWithElevatedPrivileges(delegate | |
{ | |
SPSite site; | |
SPWeb web; | |
using (new SPMonitoredScope("Open elevated web and site")) | |
{ | |
site = new SPSite(siteId); | |
try { web = site.OpenWeb(webId); } | |
catch | |
{ | |
site.Dispose(); | |
throw; | |
} | |
} | |
using (site) | |
using (web) | |
{ | |
try | |
{ | |
if (allowUnsafeUpdates) | |
{ | |
web.AllowUnsafeUpdates = true; | |
} | |
secureCode(site, web); | |
} | |
finally | |
{ | |
if (allowUnsafeUpdates) | |
{ | |
web.AllowUnsafeUpdates = false; | |
} | |
} | |
} | |
}); | |
} | |
public static void RunInElevatedSite(CodeToRunInElevatedEnvironment secureCode, Guid siteId, Guid webId) | |
{ | |
RunInElevatedSite(secureCode, siteId, webId, false); | |
} | |
public static void RunInElevatedSite(CodeToRunInElevatedEnvironment secureCode, SPSite site, SPWeb web, | |
bool allowUnsafeUpdates) | |
{ | |
RunInElevatedSite(secureCode, site.ID, web.ID, allowUnsafeUpdates); | |
} | |
public static void RunInElevatedSite(CodeToRunInElevatedEnvironment secureCode, SPSite site, SPWeb web) | |
{ | |
RunInElevatedSite(secureCode, site.ID, web.ID, false); | |
} | |
public static void RunInElevatedSite(CodeToRunInElevatedEnvironment secureCode) | |
{ | |
RunInElevatedSite(secureCode, SPContext.Current.Site.ID, SPContext.Current.Web.ID, false); | |
} | |
public static void RunInElevatedSite(CodeToRunInElevatedEnvironment secureCode, bool allowUnsafeUpdates) | |
{ | |
RunInElevatedSite(secureCode, SPContext.Current.Site.ID, SPContext.Current.Web.ID, allowUnsafeUpdates); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment