Blog

Tagged by 'datarow'

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