Dienstag, 21. Juni 2011

Using temporary HttpContext in Sharepoint

Quite often you run into the problem, that classes do use the current HttpContext with all the permissions set. I had this problem e.g. with the PortalSiteMapProvider. It uses the regular HttpContext and there is no chance to set other properties to it. In this case you can use a temporary HttpContext to give it the SPWeb you need with the permissions of a specified user. After running your code you can turn back to the regular context.

public class TemporaryHttpContext : IDisposable
{
public TemporaryHttpContext()
{
thecurrentContext = HttpContext.Current;
HttpContext.Current = null;
}

public TemporaryHttpContext(SPWeb spweb, string userlogin) : this()
{
TemporyContext(spweb, userlogin);
}

private HttpContext thecurrentContext { get; set; }

public void TemporaryContext(SPWeb spweb, string userlogin)
{
if (spweb == null || !spweb.Exists)
{
throw new ArgumentNullException("web");
}

var request = new HttpRequest("", spweb.Url, "");
HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter(CultureInfo.CurrentCulture)));

HttpContext.Current.Items["HttpHandlerSPWeb"] = spweb;

var wi = WindowsIdentity.GetCurrent();
if (wi != null)
{
FieldInfo fieldInfo = typeof(WindowsIdentity).GetField("m_name", BindingFlags.NonPublic | BindingFlags.Instance);
if (fieldInfo != null)
{
fieldInfo.SetValue(wi, userlogin);
}

HttpContext.Current.User = new GenericPrincipal(wi, new string[0]);
}
}

public void OldContext()
{
HttpContext.Current = thecurrentContext;
}

public void Dispose()
{
OldContext();
}
}


Afterwards just wrap your code into the temporary context like this:


TemporaryHttpContext tempcontext = new TemporaryHttpContext();
tempcontext.TemporaryContext(spweb, userloginname);
[Your code that needs a temporary context]
tempcontext.Dispose();

and then everything should work smoothly.

Dienstag, 7. Juni 2011

Webpart maintenance page

If you are running into the problem, that you added a webpart to your page and you want to remove it, because it breaks your page, then just add the query-string ?contents=1 to your URL and it will redirect you to the webpart maintenance page.

Webpart maintenance page

If you are running into the problem, that you added a webpart to your page and you want to remove it, because it breaks your page, then just add the query-string ?contents=1 to your URL and it will redirect you to the webpart maintenance page.