Does Kentico Cache Case-Sensitive Query String’s?

I noticed something very strange whilst working on one of my recent Kentico projects, where I required a query string value to be case-sensitive. You might be asking why? Well the plan was to pass case-sensitive Base64 random value in a bit.ly ID format. For example: www.mysite.com/Home/iAfcTy.

So I added a Wildcard URL to one of my pages to keep the URL looking nice and tidy. In this case: “/Home/{ID}”.

Kentico Document Url Path

Something with the most simplest of intensions ended up being a bit of a nightmare and to demonstrate what I experienced, see the following test-cases using Kentico’s Wildcard parameter.

Test 1

Passing “Hello” to the query string parameter resulted in the following:

Kentico Wildcard Case 1

This is the correct outcome.

Test 2 – Things get interesting!

Passing “HELLO” to the same query string parameter resulted in the following:

Kentico Wildcard Case 2

As you can see, the query string has been cached and resulted in the same value being used. It seems Kentico completely disregards the case sensitivity and it’s only by adding or removing characters that Kentico detects the value passed has changed.

***

My understanding is that by default Kentico accepts the URL’s as entered by the website user. I thought by going to CMS Site Manager and changing the URL settings to “Use exactly the URL of the document” would accept case-sensitive lettering .

Kentico Redirect Valid Urls

As it turns out through my testing, this setting under “URL's and SEO” section doesn’t fix the issue and this may only work for document page names and not the query strings values themselves.

For one moment, I thought I managed to find a bug in the Kentico platform and was hoping that I'd get a tree planted bearing my name through Kentico’s brilliant tree for a bug campaign. Alas, this was not the case. After discussing in great detail the problem with emails sent back and forth I couldn't seem to get the support personnel to replicate the issue.

But if I'm experiencing this issue across different networks, workstations and installations, there must be an underlying problem within the Kentico platform.

If one of my fellow Kentico experts can can try what I have stated in my post and report their findings in the comments section, it would be much appreciated.

Who knows, there might be a really simple thing I’ve overlooked.

Workaround

Using the standard way of passing a query string value works perfectly and it only seems Kentico Wildcard URL’s  experiences this issue. So instead of using the Wildcard method, you will have to pass values in the following format:

www.mysite.com/Home/?ID=Hello

Small Issue With “CMSPreferredCulture” Cookie When Working On A Multilingual Kentico Site Within A Development Environment

I've been playing around with creating multilingual sites in Kentico (version 5.5R2). One of (the many) Kentico strengths is being able to configure an existing site installation to cater for multi-lingual support in a straight-forward manner.

I came across a perplexing problem when trying to view both of my multilingual sites within the same browser. In my development environment I created two domain aliases and sites in IIS:

  1. Danish – http://172.16.1.28:8010
  2. British – http://172.16.1.28:8011

As you can see, I differentiate between two of my site cultures by the port number.

Kentico Domain Aliases

I could only view one version of the site on both URL’s. This was all due to a cookie that gets created on first site visit and stores the “CMSPreferredCulture” based on the domain name excluding the port number. For those who aren’t aware, “CMSPreferredCulture” simply contains a localisation code for the site. You might be thinking: What’s the big deal?

Kentico CMSPreferredCulture Cookie

Well an issue only occurs when you are developing and testing the multi-lingual setup within a local environment and don’t have access to a range of unique domains. I thought that I could use the different port numbers in my environment to distinguish between the different site cultures.

I was wrong. Kentico only uses the domain name and excludes any port  numbers. If you need to view different culture versions of your site at the same time, you will need to view them in different browsers.

A small thing like this can cause some bewilderment for a Kentico novice. It sure bewildered me. You would expect to have the ability to view different versions of a site based on the domain aliases setup in Kentico.

If anyone can suggest a work-around. Please leave a comment.

Simple Way To Retrieve A Document Attachment In Kentico

If you are storing images or files using a “Direct Uploader” field type within a document and you need to retrieve them in your code, you have two options to get this file back:

  1. Read up on the Kentico API (DocumentHelper.GetAttachment() methods).
  2. Use Kentico’s “GetFile.aspx” page to reference the file itself.

As much as I would like to do things properly and familiarise myself with the Kentico API in greater detail, project time constraints can be a hindrance. In this case, I used the “GetFile.aspx” page in the following manner:

<img src="/CMSPages/GetFile.aspx?guid=<Attachment GUID>" title="My Kentico Image" />

The GUID for “Attachment GUID” will be found in the document where you use the “Direct Uploader” field.

I don’t know if this is what you would necessarily call a hack. But it works!

Count Message board Messages for a Document in Kentico

I’ve been using Message Boards for some page templates within my Kentico site. I needed to count the number of messages for each document to be displayed on the homepage. I couldn’t find a way to do this using Kentico’s API.

The only way I could retrieve the message count for a document is to query the database directly. The SQL query is as follows.

SELECT COUNT(*)
FROM Board_Board
JOIN Board_Message ON Board_board.BoardID = Board_Message.MessageBoardID
WHERE 
Board_Message.MessageApproved = 1 AND Board_Board.BoardDocumentID = @DocumentID

 

If anyone knows how to achieve the exact same thing through the API, please leave a comment.

Add MemoryStream File To Kentico Media Library

I needed to be able to pass a file that was stored in a MemoryStream into my Kentico Media Library. In my case, the file was a dynamically generated PDF.

I couldn’t find anything on the web on how I would achieve this. So I decided to have a go creating my own method based on the Media Library API and some very basic examples, as you can see below:

public static string AddFile(MemoryStream file, string fileName, string description, string mediaLibrayName, string folder, string fileExt)
{
    string cleanFileName = MakeValidFileName(fileName).Replace(" ", "-");

    string folderDirectory = HttpContext.Current.Server.MapPath(String.Format("/MyWebsite/media/{0}/{1}/", mediaLibrayName, folder));

    string mediaFilePath = String.Format("{0}{1}.{2}", folderDirectory, cleanFileName, fileExt);

    if (!File.Exists(mediaFilePath))
    {
        #region Create File in Media Library Directory

        //Check if directory exists
        if (!Directory.Exists(folderDirectory))
            Directory.CreateDirectory(folderDirectory);

        file.Position = 0;

        FileStream outStream = new FileStream(mediaFilePath, FileMode.Create, FileAccess.Write);
        file.WriteTo(outStream);
        outStream.Flush();
        outStream.Close();

        #endregion

        #region Add file info to Kentico Media library

        //Media Library Info - takes Media Library Name and Website Name
        MediaLibraryInfo libraryInfo = MediaLibraryInfoProvider.GetMediaLibraryInfo(mediaLibrayName, CMSContext.CurrentSiteName);

        // Get Relative Path to File
        string path = CMS.MediaLibrary.MediaLibraryHelper.EnsurePath(mediaFilePath);

        //Create media file info item
        MediaFileInfo fileInfo = new MediaFileInfo(path, libraryInfo.LibraryID, folder);

        fileInfo.FileTitle = fileName;
        fileInfo.FileDescription = description;

        // Save media file info
        MediaFileInfoProvider.ImportMediaFileInfo(fileInfo);

        #endregion

        return String.Format("/MyWebsite/media/{0}/{1}/{2}.{3}?&ext=.{3}", mediaLibrayName, folder, cleanFileName, fileExt);
    }
    else
    {
        return String.Empty;
    }
}

The method I created is generic enough for you use in your own Kentico site. It provides all the necessary parameters needed  to add an image to a media library of your choice. For example:

AddFile(fileMemStream, “Marketing Issue", “Monthly Marketing Info”, "Private", "Premium Folder", "pdf");

As much as a like using the Kentico CMS platform, I find their API documentation some what lacking in examples on how to use certain methods, especially for a new Kentico developer like myself. I am hoping this is something that will change in the near future.

Output Message Board Comments for Individual Pages In Kentico

I needed to implement a message board for users to comment on individual articles stored within my Kentico site. To achieve this, I decided to use a message board. Initially, what I found when I implemented the message board web part to my article template was that submitted comments for individual articles were getting displayed on all other articles.

In my page I am using two Kentico controls: MessageBoardViewer to output the list of comments and MessageBoard for the comments form.

<%@ Register Src="/CMSWebParts/MessageBoards/MessageBoard.ascx" TagName="MessageBoard" TagPrefix="cms" %>
<%@ Register Src="/CMSWebParts/MessageBoards/MessageBoardViewer.ascx" TagName="MessageBoardViewer" TagPrefix="cms" %>

<cms:MessageBoardViewer ID="MessageBoardViewer1" runat="server" Enabled="true" HideControlForZeroRows="false" DisplayOnlyApproved="true" DisplayToRoles="Registered;Paid" ShowForDocumentTypes="NewsSite.News" ZeroRowsText="No Messages in viewer" TransformationName="Community.Transformations.MessageBoard" AlternatingItemTransformationName="Community.Transformations.MessageBoard"></cms:MessageBoardViewer>
<cms:MessageBoard ID="MessageBoard1" BoardModerated="true" runat="server" BoardUseCaptcha="false" BoardAccess="AllUsers" DisplayToRoles="Paid" BoardOpened="true" BoardRequireEmails="false"  BoardEnableSubscriptions="true" ></cms:MessageBoard>

I came across a fix on the (very informative) Kentico forums whereby a user carried out a where condition on the MessageBoardViewer control to retrieve article comments through the “BoardDisplayName” field:

MessageBoardViewer1.WhereCondition = 
    String.Concat("BoardDisplayName = '", 
                    CMSContext.CurrentDocument.GetValue("Title"), " (", 
                    CMSContext.CurrentDocument.DocumentNamePath, 
                  ")'"); 

Some of you may not know, the Board Display Name field is also used in the Message board section within CMS Desk.

Kentico Message Board Admin

Retrieving comments based on the Board Display Name is in my opinion not the best way. As you can see from the title of my document (above) contains single quotes. This would cause an SQL syntax error (which I did experience).

To get around this, it is best to query the MessageBoardViewer control using the “BoardDocumentID” field. So the code will be as follows:

MessageBoardViewer1.WhereCondition = 
    String.Concat("BoardDocumentID = ", 
                    CMSContext.CurrentDocument.DocumentID); 

If anyone knows of a better way of achieving the same thing. Please leave a comment. I am relatively new to Kentico and probably missed a trick!