fredag den 13. december 2013

Server.MapPath inside Sitecore tasks

From time to time, when developing tasks for Sitecore, you need to map a virtual path to a physical file in the file system.

The way to do this, is to call the function MapPath on the Server object located on the current HttpContext object.

This will not work in tasks, since there is no HttpContext (since we are running on a separate thread) - so we have to come up with some other way to map this, in case the HttpContext is missing.

This is the solution we have come up with:

public static string MapFilePath(string filePath)
{
    if (string.IsNullOrEmpty(filePath))
    {
        throw new ArgumentException("filePath must not be empty", "filePath");
    }

    if (HttpContext.Current != null)
    {
        return HttpContext.Current.Server.MapPath(filePath);
    }

    return HttpRuntime.AppDomainAppPath + filePath.Replace('/', '\\');
}

If there is a HttpContext, we just do like we normally do, since that is prettier - but if there is no HttpContext, we have to do some string manipulation to get the desired result.

Since the URL has to use back slashes instead of forward slashes, we have to replace those.

This results in usable URL's being returned, no matter if you are in a task or not :-)

WCF, Sitecore, JSON and .NET 4.0 vs. .NET 4.5.1

I have been having an interresting problem the last few days, that took quite a while to debug.

This doesn't only relate to Sitecore, since every WCF service created in .NET that is consumed be javascript can run into this issue.

A little while ago, we upgraded our development machines to Windows 8.1, which includes .NET 4.5.1 - which seems to make some small, but important changes to WCF.

It turns out, that Microsoft has changed how objects gets wrapped, when they are outputted as JSON.

In .NET 4.0, it seems like objects was getting wrapped in an element called "d" - this has been removed in .NET 4.5.1 (it's gone on our machines at least).

The server the website is running on, only had .NET 4.0 installed, so we got permission to upgrade it to .NET 4.5.1 , which after a little too long spinning at "Preparing to configure updates" in Windows Update, we had the server up and running with .NET 4.5.1, which fixed the problem, so the JSON nolonger is wrapped in the "d" element.

mandag den 2. december 2013

Upgrading Sitecore, Integer fields and the page editor

Just had an interresting issue with a customers solution, that we upgraded last week.

It seems, that if you have a template with a field, that is of the field type "Integer", something weird can happen.

To trigger this, you have to have three things:

  1. The field on the current page you are at.
  2. You have to be inside the page editor
  3. The field must be rendered using a <sc:text> tag.
If you do this, and then try and save the page without entering anything into the field (it might be optional for the user to enter something here, which was the case for our customer), you would get an error saying: " '' is not a valid integer.".

This happens, because Sitecore sometime between 6.5-111230 and the 6.6-130529 changed now the save event works, so it will give an error on Integer fields, even if there is no validators added to the field.

The solution we went for, was setting the field to have the default value of 0, which makes sure the field never is empty, which solved the problem.