Use the following code to run your code under elevated permission…
string url = SPContext.Current.Site.Url;
HttpContext backupCtxt = HttpContext.Current;
try
{
if (SPContext.Current != null) HttpContext.Current = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite sc = new SPSite(url))
{
using (SPWeb web = sc.OpenWeb())
{
HttpRequest httpRequest = new HttpRequest("", web.Url, "");
HttpContext.Current = new HttpContext(httpRequest, new HttpResponse(new System.IO.StringWriter()));
using (MyDataContext dc = new MyDataContext(web.Url))
{
// YOUR CODE
}
}
}
});
}
catch
{
throw;
}
finally
{
if (SPContext.Current == null)
HttpContext.Current = backupCtxt;
}
The one thing to note is that as SPContext is recreated, you will not be able to refer this in YOUR CODE. Also, any function which is called from YOUR CODE should also not use SPContext object.
0 comments:
Post a Comment