If you ever tried to set the ItemXslLink
(which is a normal property of the ContentByQueryWebPart
) you will see that sometimes this simply doesn’t work. You get an exception!
Object reference not set to an instance of an object |
Why?
The CmsDataFormWebpart.MakeSiteRelativeUrl
(which is called somewhere when setting this property) needs the HttpContext.Current
which isn’t available when using a console application or inside an event receiver (déjà vu!).
Solution
Inside the MSDN-Forums a user named AlexUC found a good workaround.
if (null == HttpContext.Current) { HttpRequest request = new HttpRequest ("", web.Url, ""); HttpContext.Current = new HttpContext (request, new HttpResponse (new StringWriter ())); HttpContext.Current.Items["HttpHandlerSPWeb"] = web; } |
Perhaps this even works when one is inside an asynchronous-event inside an event receiver?! Perhaps one can redirect the user inside the ItemAdded
-Method?!
I will analyze this soon….