<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>entwicklungsgedanken &#187; HttpContext</title>
	<atom:link href="http://www.entwicklungsgedanken.de/tag/httpcontext/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.entwicklungsgedanken.de</link>
	<description>Verschiedene Gedanken rund um die Softwareentwicklung</description>
	<lastBuildDate>Thu, 29 Jul 2010 11:09:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Problems with the ContentByQueryWebPart (CQWP) and its ItemXslLink-property</title>
		<link>http://www.entwicklungsgedanken.de/2008/04/08/problems-with-the-contentbyquerywebpart-cqwp-and-its-itemxsllink-property/</link>
		<comments>http://www.entwicklungsgedanken.de/2008/04/08/problems-with-the-contentbyquerywebpart-cqwp-and-its-itemxsllink-property/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 19:12:59 +0000</pubDate>
		<dc:creator>Eric Bartels</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[SharePoint-Development]]></category>
		<category><![CDATA[cqwp]]></category>
		<category><![CDATA[Exception]]></category>
		<category><![CDATA[HttpContext]]></category>
		<category><![CDATA[XslItemLink]]></category>

		<guid isPermaLink="false">http://www.entwicklungsgedanken.de/?p=41</guid>
		<description><![CDATA[If you ever tried to set the ItemXslLink (which is a normal property of the ContentByQueryWebPart) you will see that sometimes this simply doesn&#8217;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&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever tried to set the <code>ItemXslLink</code> (which is a normal property of the <code>ContentByQueryWebPart</code>) you will see that sometimes this simply doesn&#8217;t work. You get an exception!</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">Object</span> reference not set to an instance of an <span style="color: #FF0000;">object</span></pre></div></div>

<h4>Why?</h4>
<p>The <code>CmsDataFormWebpart.MakeSiteRelativeUrl</code> (which is called somewhere when setting this property) needs the <code>HttpContext.Current</code> which isn&#8217;t available when using a <strong>console application</strong> or inside an <strong>event receiver</strong> (<a href="http://www.entwicklungsgedanken.de/2008/03/27/redirecting-from-newformaspx-to-dispformaspx-after-creating-a-new-item/">déjà vu!</a>).</p>
<h3>Solution</h3>
<p>Inside the <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3123533&#038;SiteID=1">MSDN-Forums</a> a user named <em>AlexUC</em> found a good workaround.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span> <span style="color: #008000;">==</span> HttpContext.<span style="color: #0000FF;">Current</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    HttpRequest request <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HttpRequest <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span>, web.<span style="color: #0000FF;">Url</span>, <span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    HttpContext.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HttpContext <span style="color: #000000;">&#40;</span>request, <span style="color: #008000;">new</span> HttpResponse <span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> StringWriter <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Items</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;HttpHandlerSPWeb&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> web<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Perhaps this even works when one is inside an asynchronous-event inside an event receiver?! Perhaps one can redirect the user inside the <code>ItemAdded</code>-Method?!<br />
<strong>I will analyze this soon&#8230;</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.entwicklungsgedanken.de/2008/04/08/problems-with-the-contentbyquerywebpart-cqwp-and-its-itemxsllink-property/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Redirecting from NewForm.aspx to DispForm.aspx after creating a new item</title>
		<link>http://www.entwicklungsgedanken.de/2008/03/27/redirecting-from-newformaspx-to-dispformaspx-after-creating-a-new-item/</link>
		<comments>http://www.entwicklungsgedanken.de/2008/03/27/redirecting-from-newformaspx-to-dispformaspx-after-creating-a-new-item/#comments</comments>
		<pubDate>Thu, 27 Mar 2008 22:45:24 +0000</pubDate>
		<dc:creator>Eric Bartels</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[SharePoint-Development]]></category>
		<category><![CDATA[DispForm.aspx]]></category>
		<category><![CDATA[EventReceiver]]></category>
		<category><![CDATA[HttpContext]]></category>
		<category><![CDATA[NewForm.aspx]]></category>
		<category><![CDATA[redirect]]></category>

		<guid isPermaLink="false">http://www.entwicklungsgedanken.de/2008/03/27/redirecting-from-newformaspx-to-dispformaspx-after-creating-a-new-item/</guid>
		<description><![CDATA[The requirement Recently a simple requirement came up: &#8220;Redirect the user to the view-form of an item after he creates a new item&#8221;. Sounds simple, right? &#8230; The default behavior of SharePoint is to redirect the user to the url defined by the Source-parameter. If this parameter is not given the default view (mostly AllItems.aspx) [...]]]></description>
			<content:encoded><![CDATA[<div class="lang-en"></div>
<h3>The requirement</h3>
<p>Recently a simple requirement came up: &#8220;Redirect the user to the view-form of an item after he creates a new item&#8221;. Sounds simple, right? &#8230;</p>
<p>The default behavior of SharePoint is to redirect the user to the url defined by the <code>Source</code>-parameter. If this parameter is not given the default view (mostly <code>AllItems.aspx</code>) will be used.</p>
<h3>The result</h3>
<p>The implementation of the requirement can be seen in this short screencast. After the user creates a new item he will be redirected to review the item-properties.</p>
<div id="media">
            <object id="csSWF" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="600" height="400" codebase="http://active.macromedia.com/flash7/cabs/ swflash.cab#version=9,0,28,0"><param name="src" value="http://www.entwicklungsgedanken.de/wp-content/uploads/2008/03/RedirectDemo.swf"/><param name="bgcolor" value="#1a1a1a"/><param name="quality" value="best"/><param name="allowScriptAccess" value="always"/><param name="allowFullScreen" value="true"/><param name="scale" value="showall"/><param name="flashVars" value="autostart=false"/><embed name="csSWF" src="http://www.entwicklungsgedanken.de/wp-content/uploads/2008/03/RedirectDemo.swf" width="600" height="400" bgcolor="#1a1a1a" quality="best" allowScriptAccess="always" allowFullScreen="true" scale="showall" flashVars="autostart=false" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>
        </div>
<h3>The way it should be</h3>
<p>Not amazed by the pictures you just saw? One would think its easy to achieve this behavior. Simply add an EventReceiver to the desired list and listen to the <code>ItemAdded</code>-event and redirect the user&#8230;<br />
<strong>In theory</strong> this is should work. The problem is that there is no <code>HttpContext.Current</code> available inside the <code>ItemAdded</code>-method.</p>
<h3>The HttpContext</h3>
<p>The only way to get the current <code>HttpContext</code> is within the constructor of your custom <code>EventReceiver</code> during a <del datetime="2008-05-08T19:29:52+00:00"><strong>asynchronous event</strong></del> <strong>synchronous event</strong> (<code>ItemAdding, ItemUpdating</code>) as shown in the following pseudo code.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomEventReceiver <span style="color: #008000;">:</span> SPItemEventReceiver
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> HttpContext _currentContext <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> CustomEventReceiver <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span> <span style="color: #008000;">!=</span> HttpContext.<span style="color: #0000FF;">Current</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _currentContext <span style="color: #008000;">=</span> HttpContext.<span style="color: #0000FF;">Current</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ItemAdding <span style="color: #000000;">&#40;</span>SPItemEventProperties properties<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
       <span style="color: #008080; font-style: italic;">// Here one can use _currentContext and redirect ...</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>But now another problem arises. If you redirect the current thread is aborted and the item is never added to the list/library</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Will abort the thread</span>
_currentContext.<span style="color: #0000FF;">Redirect</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;http://somewhere/to/go&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Will NOT abort the thread but the redirection is ignored</span>
_currentContext.<span style="color: #0000FF;">Redirect</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;http://somewhere/to/go&quot;</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// What a mess!</span></pre></div></div>

<h3>The magic</h3>
<p>The only possible solution is to &#8220;manually&#8221; add the item so the redirection works as shown in the next code-block. The manual addition of the new item is necessary because the <code>properties.ListItemId</code> within <code>ItemAdding</code> is 0. But we need this ID to build the destination url in the form e.g. <code>http://hostname/web/listname/DispForm.aspx?ID=22</code></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomEventReceiver <span style="color: #008000;">:</span> SPItemEventReceiver
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Code omitted ... see above</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ItemAdding <span style="color: #000000;">&#40;</span>SPItemEventProperties properties<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Get a &quot;reference&quot; to the list</span>
        SPSite siteColl <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPSite <span style="color: #000000;">&#40;</span>properties.<span style="color: #0000FF;">SiteId</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        SPWeb site <span style="color: #008000;">=</span> siteColl.<span style="color: #0000FF;">OpenWeb</span> <span style="color: #000000;">&#40;</span>properties.<span style="color: #0000FF;">RelativeWebUrl</span><span style="color: #000000;">&#41;</span>
        SPList list <span style="color: #008000;">=</span> site.<span style="color: #0000FF;">Lists</span><span style="color: #000000;">&#91;</span>properties.<span style="color: #0000FF;">ListId</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Add the item and fill it with the values from properties</span>
        DisableEventFiring <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        SPListItem itemToAdd <span style="color: #008000;">=</span> list.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Add</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">// ... </span>
        EnableEventFiring <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Cleanup</span>
        site.<span style="color: #0000FF;">Dispose</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        siteColl.<span style="color: #0000FF;">Dispose</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Redirect</span>
        SPUtility.<span style="color: #0000FF;">Redirect</span> <span style="color: #000000;">&#40;</span>targetUrlOfNewItem,
            SPRedirectFlags.<span style="color: #0600FF;">Default</span>, _currentContext<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>Hint:</strong> Because the form used to display an item can change you should retrieve it via the <code>SPList.Forms[PAGETYPE.PAGE_DISPLAYFORM]</code>-property.</p>
<h3>Conclusion</h3>
<p>The <code>HttpContext.Current</code> is only available within the constructor of your event receiver when handling synchronous events like <code>ItemAdding, ItemUpdating</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.entwicklungsgedanken.de/2008/03/27/redirecting-from-newformaspx-to-dispformaspx-after-creating-a-new-item/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
	</channel>
</rss>
