Preserve Session State when using Server.Transfer in Sitecore
The title of this post is a little misleading, the solution doesn't use Server.Transfer, it provides an alternate solution involving the Sitecore.Web.WebUtil.RewriteUrl() method which relies on HttpContext.RewritePath() method.
This solution may not fit the purpose for everyone or every purpose, but here goes:
This - as far as I can tell - needs to be in the httpRequestBegin pipeline before the page starts rendering. For my purpose, I was trying to use the Sitecore setting RequestErrors.UseServerSideRedirect=true so the url of the requested item doesn't change on a 404 error (ItemNotFound). I needed to override the PerformRedirect method in Sitecore.Pipelines.HttpRequest.ExecuteRequest class. To do so I created a new class that would be replacing this one in the httpRequestBegin pipeline. However, you don't need to replace the Sitecore.Pipelines.HttpRequest.ExecuteRequest class, you can create your own process and add it to the end of the httpRequestBegin pipeline.
public class ExecuteRequest : Sitecore.Pipelines.HttpRequest.ExecuteRequest
{
protected override void PerformRedirect(string url)
{
if (Settings.RequestErrors.UseServerSideRedirect)
{
Sitecore.Web.WebUtil.RewriteUrl(url);
}
else
{
WebUtil.Redirect(url, false);
}
}
}
Note that this will only work if the URL is to an .aspx page and in the page load method set the Sitecore.Context.Item to whichever item you'd like. This solution will not work for Sitecore Items.
If you'd like to server side redirect to a Sitecore Item, please find out how to do that here.
Now you have to replace the in the web.config, you can do it by modifying the web.config directly OR if you're like me and would like to keep the web.config file as close to the release version as possible, then you can add a .config file to the /App_Config/Include folder with something similar to the below XML.
your.namespace.ExecuteRequest, yourAssembly
Now the page you previously tried to Server.Transfer to that didn't have state, now has session state!
Need to fire onload event on script element?
I've recently had a client request to have a "desktop quote" page that used a third party API to get stock quote information in a popup. The interesting thing about the API is to access the data you modified the src of a script tag so it would return javascript that created an object that you could use to access the stock quote data.
I have a dropdown list of possible comparison quotes to be loaded dynamically. I attach an onchange event handler to it so I can refresh the page with the appropriate query string (need to refresh page every 15 minutes to get latest data). Here is where I add the event handler.
dojo.query("#bench").connect("onchange", this, function(e) {
var url = getUrl();
if(url != null)
window.location = url + "?ticker=" + e.target.value;
});
On page load using dojo.addOnLoad(), I call the below code if there is a query string parameter named ticker and the value is valid. This snippet adds a script tag to the DOM under the body tag. Then attaches an onload event to the script element.
var n = dojo.create("script", {
type: "text/javascript",
src: "http://apps.shareholder.com/irxml/irxml.aspx?COMPANYID=abc&PIN=abc123&FUNCTION=StockQuote&OUTPUT=js2&TICKER=" + ticker
}, dojo.body(), "first")
if (dojo.isIE)
dojo.query(n).connect("onreadystatechange", this, function() {
populateData("Comp");
});
else
dojo.query(n).connect("onload", this, function() {
populateData("Comp");
});
Notice the if(dojo.isIE)? The onload event is not fired in IE for the script element (works fine in firefox and chrome). However, the onreadystatechange event is. This ensures that the objects/code in the the dynamically injected javascript gets executed before the populateData function is called.
Visual Studio Unable to add to the Web site Unable to add file Access is denied
I am trying to publish my project from my development machine to the staging environment. I would right click the project in visual studio and click publish. Most of the files would publish just fine, but a few were giving me problems. In the output log, there were multiple error messages, all stating:
Unable to add ‘XXX.ext’ to the Web site. Unable to add file ‘XXX.ext’. Access is denied.
Sitecore – Encoding Special Characters In Fields
If web standards compliance is one of your top priorities and you have a site core implementation, then you may want to continue reading. Are you tired of seeing & instead of & on your site? Not sure of the best way to encode them? I have created a processor that snaps into the renderField pipeline. The only caveat is that in order for this code to run you need to use the FieldRenderer.Render(Item, FieldName) method. Which, in my opinion, you should always be using anyway.
Sitecore – FieldRenderer.Render(item, fieldname) returning “” empty string
Today I spent 2 hours trying to debug why FieldRenderer.Render() was returning an empty string.
It was working yesterday and the code didn't change, so what was it?