fredag den 4. september 2015

How to write data from Sitecore (an other sources) to a CSV file that Excel understands

From time to time, our customers wants to export data from Sitecore to CSV, to be able to view it in Excel.

Depending on where in the world you live, this might not be an issue for you, since people in the english speaking countries will just be using ASCII and there will be no issues.

But, if you like me live in one of the nordic countries (or alot of other places), you will be running into having to deal with text with letters, such as 'æ', 'ø', 'å' among others.

If you write strings containing any of these charaters into the CSV file just like that, it will come out malformed when the file is opened in Excel.

This is due to the encoding of the file, which confuses it.

Here is how to do it the easy way:

public static class CsvWriter
{
    public static void WriteToCSVFile(string[][] dataRows, string fileName)
    {
        using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                WriteByteOrderMarker(writer);

                foreach (string[] dataRow in dataRows)
                {
                    WriteLineSegments(writer, dataRow);
                }
            }
        }
    }

    private static void WriteByteOrderMarker(BinaryWriter writer)
    {
        byte[] BOM = { 0xef, 0xbb, 0xbf };

        writer.Write(BOM);
        writer.Flush();
    }
            
    private static void WriteLineSegments(BinaryWriter writer, string[] lineSegments)
    {
        writer.Write(Encoding.UTF8.GetBytes(string.Join(";", lineSegments) + "\r\n"));
        writer.Flush();
    }
}

First save the data in the 2D string array, and call the WriteToCSVFile function with the filename to save the data to.

First a byte order mark is written, which tells Excel (and other applications), that the content of the file is UTF-8 encoded.
Then, when each line is written, it gets encoded, so it matches.

That way, all programs should be able to read it, as long as they understand the BOM in the start of the file :-)

When WFFM forgets to include Checkbox List fields in Send Email save actions

Okay, so one of your editors just created this great new form using the Sitecore WFFM module, and it looks great!

But then you want to make it send an email when the user submits the form - and all the sudden, the great looking form generates broken emails.

The problem is, that there is a Checkbox List field in the form, and for some weird reason, the Send Email save action does not care about that field type, so it just ignores it completly.

But, the help is near (no, not Ghostbusters this time) - just contact Sitecore support, and ask for the support package attached to the issue #442870 .

Simple and easy, right? :-)

Just upgraded, and now the page editor is broken? This might be why!

A little while ago, I finished upgrading a customers solution from Sitecore 6.4.1 to Sitecore 6.6, and all the sudden, the page editor stopped working. (The editors could not enter any text into any of the text areas)

It turns out, that there is a small note in the release notes for Sitecore 6.5 that mentions, that renderings and top level text notes should be wrapped in a div or span tag.

This can also happen if there is a sitecore placeholder placed at the top level of a usercontrol - so in this case, you also need to wrap it in a div tag.

So here you have it - it's a small thing to do, but it took quite a while to figure it out - hopefully this can save you from that, if you run into the same issue.