Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Charles 42 posts 63 karma points
    Mar 11, 2014 @ 03:55
    Charles
    0

    How to Get a List of Header Document Types for User Control to Grab all Children.OfType<Header>

    I am trying to get a list of Header Document Types (that have several properties attached to the doc type) from within Umbraco to be outputted on the page, but with my code I get nothing returned at all, and I have created 4 Header Document Types, with different images under the Home Document Type (Umbraco Admin Content area). Previously the .cs file Home.cs only allowed for the HomeGroup Document Type, but I attempted to add in support for Header Document Type also. So, following the structure of the current Home.cs file that grabs all HomeGroup Document Types, I have edited the Home.cs file like so (didn't bother adding in the using at the very top, cause I'm sure they are all right):

    namespace MMG.BusinessLayer
    {
        public class Home
        {
            #region Data Declarations
    
            private static object   _Lock               = "";
            private int             _ID                 = 0;
            private SEO             _SEO                = new SEO();
            List<HomeGroup>         _HomeGroupList      = new List<HomeGroup>();
            List<Header>            _HomeHeaderList     = new List<Header>();
    
            #endregion
    
            #region Constructor
            public Home () 
            {
    
            }
            #endregion
    
            #region Public Static Methods
            public static Home GetCached (Node aNode) 
            {
                Home theHome = new Home();
    
                try
                {
                    string theCacheKey = "Home:" + aNode.Id;
    
                    if (System.Web.Hosting.HostingEnvironment.Cache[theCacheKey] == null)
                    {
                        lock (_Lock)
                        {
                            if (System.Web.Hosting.HostingEnvironment.Cache[theCacheKey] == null)
                            {
                                theHome.Parse(aNode);
    
                                System.Web.Hosting.HostingEnvironment.Cache.Insert(theCacheKey, theHome);
    
                                UmbracoHelper.AddCacheKey(theCacheKey);
                            }
                        }
                    }
    
                    theHome = (Home)System.Web.Hosting.HostingEnvironment.Cache[theCacheKey];
                }
                catch (Exception ex)
                {
                    ErrorHelper.LogError("Home:GetCached", ex);
                }
    
                return theHome;
            }
            #endregion
    
            #region Private Static Methods
            private Home Parse (Node aNode) 
            {
                Home theItem = new Home();
    
                try
                {
                    ID  = aNode.Id;
                    SEO = SEO.Parse(aNode);
                    HomeGroupList.GetList(aNode);
    
                    foreach (Node aChildNode in aNode.Children)
                    {
                        if (aChildNode.NodeTypeAlias == "Header")
                        {
                            Header theHeader = new Header();
    
                            if (theHeader.Parse(aChildNode))
                            {
                                theItem.HomeHeaderList.Add(theHeader);
                            }
                        }
                    }
    
                }
                catch (Exception ex)
                {
                    ErrorHelper.LogError("Home:Parse", ex);
                }
    
                return theItem;
            }
    
            #endregion
    
            #region Public Properties
            public int ID  
            {
                get
                {
                    return _ID;
                }
                set
                {
                    _ID = value;
                }
            }
            public SEO SEO 
            {
                get
                {
                    return _SEO;
                }
                set
                {
                    _SEO = value;
                }
            }
            public List<HomeGroup> HomeGroupList
            {
                get
                {
                    return _HomeGroupList;
    ;
                }
                set
                {
                    _HomeGroupList = value;
                }
            }
            public List<Header> HomeHeaderList
            {
                get
                {
                    return _HomeHeaderList;
                }
                set
                {
                    _HomeHeaderList = value;
                }
            }
    
            #endregion
        }
    }
    

    So, basically, I am trying to be able to use this and get a list of all of the Children of the Home Document Type within Umbraco that are of type Header. I created a HomeHeaderList method in here that needs to get a list of all nodes that are direct children of the Home Node and are TypeOf<Header>. Did I do something wrong here??

    The Header.cs file was already created and looks like this:

    namespace MMG.BusinessLayer
    {
        public class Header
        {
            #region Data Declarations
            private static object   _Lock           = new object();
            private Node            _Node           = null;
            private int             _ID             = 0;
            private string          _Image          = string.Empty;
            private string          _ThumbImage     = string.Empty;
            private string          _Caption        = string.Empty;
            private string          _LinkText       = string.Empty;
            private string          _Link           = string.Empty;
            private string          _LinkTarget     = string.Empty;
            private string          _LinkOnClick    = string.Empty;
            private string          _Credit         = string.Empty;
            #endregion
    
            #region Constructor
            public Header()
            {
    
            }
            public Header(Node aNode)
            {
                try
                {
                    _Node = aNode;
                    _ID = (int)aNode.Id;
    
                    Parse(aNode);
                }
                catch (Exception ex)
                {
                    ErrorHelper.LogError("Header", ex);
                }
            }
            #endregion
    
            #region Public Static Methods
            public static Header GetCached(Node aNode)
            {
                Header theItem = new Header();
    
                try
                {
                    string theCacheKey = "Header:" + aNode.Id;
    
                    if (System.Web.Hosting.HostingEnvironment.Cache[theCacheKey] == null)
                    {
                        lock (_Lock)
                        {
                            if (System.Web.Hosting.HostingEnvironment.Cache[theCacheKey] == null)
                            {
                                theItem.Parse(aNode);
    
                                System.Web.Hosting.HostingEnvironment.Cache.Insert(theCacheKey, theItem);
    
                                UmbracoHelper.AddCacheKey(theCacheKey);
                            }
                        }
                    }
    
                    theItem = (Header)System.Web.Hosting.HostingEnvironment.Cache[theCacheKey];
                }
                catch (Exception ex)
                {
                    ErrorHelper.LogError("Header:GetCached", ex);
                }
    
                return theItem;
            }
            public static Header ParseDocument(Document aDocument)
            {
                Header theHeader = new Header();
    
                theHeader.ID = aDocument.Id;
    
                if(aDocument.getProperty("caption") != null)
                {
                    theHeader.Caption = aDocument.getProperty("caption").Value.ToString();
                }            
                if(aDocument.getProperty("linkText") != null)
                {
                    theHeader.LinkText = aDocument.getProperty("linkText").Value.ToString().ReplaceLinks().Trim();
                }
                if(aDocument.getProperty("credit") != null)
                {
                    theHeader.Credit = aDocument.getProperty("credit").Value.ToString();
                }    
                return theHeader;
            }
            public static Document ToDocument(XmlNode aXmlNode)
            {
                int theID = 0;
    
                XmlNode theIDNode = aXmlNode.SelectSingleNode("ID"); 
                XmlNode theCaptionNode = aXmlNode.SelectSingleNode("Caption");
                XmlNode theLinkTextNode = aXmlNode.SelectSingleNode("LinkText");
                XmlNode theCreditNode = aXmlNode.SelectSingleNode("Credit"); 
    
                if (theIDNode != null) int.TryParse(theIDNode.InnerText, out theID);
    
                if (theID > 0)
                {
                    Document theDocument = new Document(theID);
    
                    theDocument.getProperty("caption").Value = theCaptionNode == null ? "" : theCaptionNode.InnerText;
                    theDocument.getProperty("linkText").Value = theLinkTextNode == null ? "" : theLinkTextNode.InnerText;
                    theDocument.getProperty("credit").Value = theCreditNode == null ? "" : theCreditNode.InnerText; 
    
                    return theDocument;
                }
    
                return null;
            }
            #endregion
    
            #region Public Methods
            public bool Parse(Node aNode)
            {
                try
                {
                    _ID = aNode.Id;
    
                    int theMediaID = 0;
    
                    if (aNode.GetProperty("caption") != null)
                    {
                        _Caption = aNode.GetProperty("caption").Value;
                    }
                    if (int.TryParse(aNode.GetProperty("image").Value, out theMediaID))
                    {
                        if (theMediaID != 0)
                        {
                            Media theMedia = new Media(theMediaID);
    
                            _Image = theMedia.getProperty("umbracoFile").Value.ToString();
                        }
                    }
    
                    theMediaID = 0;
                    if (int.TryParse(aNode.GetProperty("thumbImage").Value, out theMediaID))
                    {
                        if (theMediaID != 0)
                        {
                            Media theMedia = new Media(theMediaID);
    
                            _ThumbImage = theMedia.getProperty("umbracoFile").Value.ToString();
                        }
                    }
                    if (aNode.GetProperty("caption") != null)
                    {
                        _Caption = aNode.GetProperty("caption").Value;
                    }
                    if (aNode.GetProperty("link") != null)
                    {
                        _Link = aNode.GetProperty("link").Value;
                    }
                    if (aNode.GetProperty("linkText") != null)
                    {
                        _LinkText = aNode.GetProperty("linkText").Value;
                    }
                    if (aNode.GetProperty("linkOnClick") != null)
                    {
                        _LinkOnClick = aNode.GetProperty("linkOnClick").Value;
                    }                
                    if (aNode.GetProperty("linkTarget") != null)
                    {
                        switch (aNode.GetProperty("linkTarget").Value)
                        {
                            case "New Window": 
                                _LinkTarget = "_blank"; 
                                break;
                            case "Current Window":
                                _LinkTarget = "_self";
                                break;
                        }
                    }
                    if (aNode.GetProperty("credit") != null)
                    {
                        _Credit = aNode.GetProperty("credit").Value;
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    ErrorHelper.LogError("Header:Parse", "Content ID: " + _ID + ", Alias: " + HttpContext.Current.Request.RawUrl + ex);
    
                    return false;
                }
            }
            #endregion
    
            #region Public Properties
            [XmlIgnoreAttribute]
            public static object Lock
            {
                get
                {
                    return _Lock;
                }
                set
                {
                    _Lock = value;
                }
            }
            [XmlIgnoreAttribute]
            public Node Node
            {
                get
                {
                    return _Node;
                }
                set
                {
                    _Node = value;
                }
            }
            public int ID
            {
                get
                {
                    return _ID;
                }
                set
                {
                    _ID = value;
                }
            }
            [XmlIgnoreAttribute]
            public string Image
            {
                get
                {
                    return _Image;
                }
                set
                {
                    _Image = value;
                }
            }
            [XmlIgnoreAttribute]
            public string ThumbImage
            {
                get
                {
                    return _ThumbImage;
                }
                set
                {
                    _ThumbImage = value;
                }
            }
            public string Caption
            {
                get
                {
                    return _Caption;
                }
                set
                {
                    _Caption = value;
                }
            }
            public string LinkText    
            {
                get
                {
                    return _LinkText;
                }
                set
                {
                    _LinkText = value;
                }
            }
            [XmlIgnoreAttribute]
            public string Link       
            {
                get
                {
                    return _Link;
                }
                set
                {
                    _Link = value;
                }
            }
            [XmlIgnoreAttribute]
            public string LinkTarget  
            {
                get
                {
                    return _LinkTarget;
                }
                set
                {
                    _LinkTarget = value;
                }
            }
            [XmlIgnoreAttribute]
            public string LinkOnClick
            {
                get
                {
                    return _LinkOnClick;
                }
                set
                {
                    _LinkOnClick = value;
                }
            }
            public string Credit
            {
                get
                {
                    return _Credit;
                }
                set
                {
                    _Credit = value;
                }
            }
            #endregion
        }
    }
    

    Ok, so the purpose of this is for people to add as many Headers as they need for Hero Images to the Home Page that will populate a slider effect. This will get called within a User Control HeroControl.ascx and HeroControl.ascx.cs

    HeroControl.ascx looks like this:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="HeroControl.ascx.cs" Inherits="controls_HeroControl" %>
    <%@ Register TagPrefix="mmg" TagName="WidgetControl"    Src="~/controls/WidgetControl.ascx"  %>
    <%@ Import Namespace="MMG.BusinessLayer" %>
    <asp:PlaceHolder ID="TheHeroHomePH" runat="server" Visible="false">
        <div class="slider">
        <asp:Repeater ID="TheHeroHomeRepeater" runat="server">
                <ItemTemplate>
            <div class="slide"><a href="<%# ((Header)Container.DataItem).Link %>" target="<%# ((Header)Container.DataItem).LinkTarget %>" onclick="<%# ((Header)Container.DataItem).LinkOnClick %>"><img src="<%# ((Header)Container.DataItem).Image %>" alt="<%# ((Header)Container.DataItem).LinkText %>" /></a></div>
            </ItemTemplate>
        </asp:Repeater>
        </div>
    </asp:PlaceHolder>
    

    HeroControl.ascx.cs, for testing, looks like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using MMG.PresentationLayer;
    using MMG.BusinessLayer;
    using umbraco.NodeFactory;
    
    Home    theHome     = Home.GetCached(Context.GetContent());
    
    protected void Page_Load(object sender, EventArgs e)
    {       
        if (theHome.HomeHeaderList.Count > 0)
        {
        TheHeroHomePH.Visible = true;
        TheHeroHomeRepeater.DataSource = theHome.HomeHeaderList;
        TheHeroHomeRepeater.DataBind();
        }
    }
    

    Running this code, I get empty Homepage. No errors, just empty homepage with nothing at all on it. Am Using Umbraco 4.6.1, and like I said, I do have 4 Header Document Types created, all with different Hero Images attached to them, as Direct Children of the Home Document Type.

    I believe that whatever I'm doing wrong must be within the Home.cs file, but where and what have I forgotten here?

  • Charles 42 posts 63 karma points
    Mar 11, 2014 @ 04:07
    Charles
    0

    Just another note, only files I even edited here are Home.cs and the user control .ascx and .ascx.cs, which only includes the HomeHeaderList and changes the properties within the .ascx file for the Header.cs element, which are the same exact Alias Names of the properties set for that Document Type (Header), in Umbraco. The problem is that the Home Document type was set to output only Children that were of type HomeGroup (which has another .cs file altogether), but I need to include Header Doc Types as well, so I think I missed something here that allows for including Header Document Types as well. The Umbraco Admin is all set up properly, so it must be something in the code!

Please Sign in or register to post replies

Write your reply to:

Draft