Blog

Categorised by 'Kentico'.

  • 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.

  • 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!

  • 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.

  • 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.

  • 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!