Blog

Posts written in February 2016

Kentico Contact Management: Update Visitor Information In Code

Posted in: Kentico

Over the last few months, I've been working away on a Kentico 9 EMS project. Very exciting indeed! Really pulling out all the stops on trying to make an amazing site in an EMS framework alongside the whole team at Syndicut.

This week, I had to develop a custom built form to grab some user's information. Unfortunately, I was unable to use Kentico's Form Builder due to the requirement where the details had to be submitted to an external API, as well as utilising a custom design.

One of the many benefits you get out-the-box from using the Kentico Form Builder is the ability to log marketing activity to the Contact Management area to store information on the visitors who view and interact with your site. By building a custom form, I was losing losing key data, such as: First Name, Last Name Email Address and Telephone No. - The base key criteria of turning an anonymous visitor to a person with some context.

To avoid this loss of data, I created a method that uses the CMS.OnlineMarketingContext class to allow you to manually update the current visitor information:

/// <summary>
/// Creates/Updates a contact in Kentico's Contact Management system.
/// </summary>
/// <param name="firstName">Visitors first name</param>
/// <param name="lastName">Visitors last name</param>
/// <param name="emailAddress">Visitors email address</param>
/// <param name="businessPhone">Visitors business phone number</param>
/// <param name="allowContactOverwrite">Overwrite existing contact fields with new information.</param>
public static void SetMarketingContact(string firstName, string lastName, string emailAddress, string businessPhone, bool allowContactOverwrite = false)
{
    ContactInfo existingContact = OnlineMarketingContext.GetCurrentContact();
            
    // Update an existing contact or create a new one if no existing contact exists.
    if (existingContact != null)
    {
        existingContact.ContactFirstName = firstName;
        existingContact.ContactLastName = lastName;
        existingContact.ContactEmail = emailAddress;
        existingContact.ContactBusinessPhone = businessPhone;

        if (allowContactOverwrite)
            existingContact.Update();
    }
}

My code only shows a handful of fields that I am updating, but the "ContactInfo" class provides access to many more.

Extending Kentico DataHelper GetDataRowValue() and GetDataRowViewValue()

Posted in: Kentico

After a long blogging hiatus, I decided to make my somewhat grand return! So here we go...

There are times when you need to retrieve documents from using the TreeProvider.SelectNodes() that can return DataRowViews or DataRows of node information. Now, Kentico provides "DataHelper.GetDataRowViewValue()" and "DataHelper.DataRowValue()" methods to output your required fields of information. But I find these DataHelper methods quite tedious when you have a massive collection of node data. I am lazy and would do anything to make my life easier.

So I created a DataHelperExtension class object that would allow me to get my commonly used DataRow and DataRowView fields with ease:

  • Document ID
  • Class Name
  • Node Alias Path
  • Get String Value (for custom fields)
  • Get Integer Value (for custom fields)
  • Page Menu Name
  • Node Has Children
public static class DataHelperExtensions
{
    /// <summary>
    /// Get the Document ID.
    /// </summary>
    /// <param name="dr"></param>
    /// <returns></returns>
    public static int DocumentId(this DataRow dr)
    {
        return int.Parse(DataHelper.GetDataRowValue(dr, "DocumentID").ToString());
    }

    /// <summary>
    /// Get the document Class Name.
    /// </summary>
    /// <param name="dr"></param>
    /// <returns></returns>
    public static string ClassName(this DataRow dr)
    {
        return DataHelper.GetDataRowValue(dr, "ClassName").ToString();
    }
        
    /// <summary>
    /// Get Node Alias Path.
    /// </summary>
    /// <param name="dr"></param>
    /// <param name="fullUrlPath"></param>
    /// <returns></returns>
    public static string NodeAliasPath(this DataRow dr, bool fullUrlPath = true)
    {
        if (!fullUrlPath)
            return DataHelper.GetDataRowValue(dr, "NodeAliasPath").ToString();
        else
            return DocumentURLProvider.GetUrl(DataHelper.GetDataRowValue(dr, "NodeAliasPath").ToString());
    }

    /// <summary>
    /// Get custom string field.
    /// </summary>
    /// <param name="dr"></param>
    /// <param name="fieldName"></param>
    /// <returns></returns>
    public static string GetStringValue(this DataRow dr, string fieldName)
    {
        if (DataHelper.GetDataRowValue(dr, fieldName) != null)
            return DataHelper.GetDataRowValue(dr, fieldName).ToString();
        else
            return String.Empty;
    }
        
    /// <summary>
    /// Get custom integer field.
    /// </summary>
    /// <param name="dr"></param>
    /// <param name="fieldName"></param>
    /// <returns></returns>
    public static int GetIntegerValue(this DataRow dr, string fieldName)
    {
        if (DataHelper.GetDataRowValue(dr, fieldName) != null)
            return int.Parse(DataHelper.GetDataRowValue(dr, fieldName).ToString());
        else
            return 0;
    }
        
    /// <summary>
    /// Get Menu Caption of a page, otherwise default to the Document Name.
    /// </summary>
    /// <param name="dr"></param>
    /// <returns></returns>
    public static string PageMenuName(this DataRow dr)
    {
        string menuCaption = DataHelper.GetDataRowValue(dr, "DocumentMenuCaption").ToString();

        if (String.IsNullOrEmpty(menuCaption))
            menuCaption = DataHelper.GetDataRowValue(dr, "DocumentName").ToString();

        return menuCaption;
    }

    /// <summary>
    /// Check if node has children.
    /// </summary>
    /// <param name="dr"></param>
    /// <returns></returns>
    public static bool NodeHasChildren(this DataRow dr)
    {
        return bool.Parse(DataHelper.GetDataRowValue(dr, "NodeHasChildren").ToString());
    }

    /// <summary>
    /// Get the Document ID.
    /// </summary>
    /// <param name="drv"></param>
    /// <returns></returns>
    public static int DocumentId(this DataRowView drv)
    {
        return int.Parse(DataHelper.GetDataRowViewValue(drv, "DocumentID").ToString());
    }

    /// <summary>
    /// Get the document Class Name.
    /// </summary>
    /// <param name="drv"></param>
    /// <returns></returns>
    public static string ClassName(this DataRowView drv)
    {
        return DataHelper.GetDataRowViewValue(drv, "ClassName").ToString();
    }

    /// <summary>
    /// Get Node Alias Path.
    /// </summary>
    /// <param name="drv"></param>
    /// <param name="fullUrlPath"></param>
    /// <returns></returns>
    public static string NodeAliasPath(this DataRowView drv, bool fullUrlPath = true)
    {
        if (!fullUrlPath)
            return DataHelper.GetDataRowViewValue(drv, "NodeAliasPath").ToString();
        else
            return DocumentURLProvider.GetUrl(DataHelper.GetDataRowViewValue(drv, "NodeAliasPath").ToString());
    }

    /// <summary>
    /// Get custom string field.
    /// </summary>
    /// <param name="drv"></param>
    /// <param name="fieldName"></param>
    /// <returns></returns>
    public static string GetStringValue(this DataRowView drv, string fieldName)
    {
        if (DataHelper.GetDataRowViewValue(drv, fieldName) != null)
            return DataHelper.GetDataRowViewValue(drv, fieldName).ToString();
        else
            return String.Empty;
    }

    /// <summary>
    /// Get custom integer field.
    /// </summary>
    /// <param name="drv"></param>
    /// <param name="fieldName"></param>
    /// <returns></returns>
    public static int GetIntegerValue(this DataRowView drv, string fieldName)
    {
        if (DataHelper.GetDataRowViewValue(drv, fieldName) != null)
            return int.Parse(DataHelper.GetDataRowViewValue(drv, fieldName).ToString());
        else
            return 0;
    }

    /// <summary>
    /// Get Menu Caption of a page, otherwise default to the Document Name.
    /// </summary>
    /// <param name="drv"></param>
    /// <returns></returns>
    public static string PageMenuName(this DataRowView drv)
    {
        string menuCaption = DataHelper.GetDataRowViewValue(drv, "DocumentMenuCaption").ToString();

        if (String.IsNullOrEmpty(menuCaption))
            menuCaption = DataHelper.GetDataRowViewValue(drv, "DocumentName").ToString();

        return menuCaption;
    }

    /// <summary>
    /// Check if node has children.
    /// </summary>
    /// <param name="drv"></param>
    /// <returns></returns>
    public static bool NodeHasChildren(this DataRowView drv)
    {
        return bool.Parse(DataHelper.GetDataRowViewValue(drv, "NodeHasChildren").ToString());
    }
}

Here's an example on how to put this DataHelperExtension class into use:

if (!DataHelper.DataSourceIsEmpty(data))
{
    DataView sortedData = data.Tables[0].DefaultView;

    foreach (DataRowView drvTestimonial in sortedData)
    {
        int docId = drvTestimonial.DocumentId(); // Get the Document ID
        string title = drvTestimonial.GetStringValue("Title"); // Custom field

        // Do something with the fields of data.
    }
}

Simple and effective!

;