Copied to clipboard

Flag this post as spam?

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


  • Rob Stevenson-Leggett 38 posts 110 karma points
    Mar 09, 2010 @ 13:26
    Rob Stevenson-Leggett
    0

    Repeatable Custom Content V2 and umbraco.library.GetItem

    Hi,

    I'm using Repeatable Custom Content V2 to create a Top 5 type of article on a site.

    This all works fine, however I also have the requirement of search listings with an image. I want to show the image from the first item in my repeatable custom content. The xml looks something like this:

    <items>
       <item>
           <data alias="image">SomeUrl</data>
           <data alias="heading">Some text</data>
      </item>
      <!--- Some more items -->
    </items>

    So I'm using umbraco examine to do the searching for me, I then need to convert it to a SearchListing POCO, a list of which is bound to a repeater at the front end.

    This works fine for my other article types as I can retreive the image url from these types using the umbraco library. However, when I try to do the following for a Top 5 Article, I get string.Empty back from umbraco.library.GetItem

    Any ideas?

    N.B. I have to do this server side as I am joining these results to results from other data sources.

    Cheers,

    Rob

    //Heres all the relevant code (from various classes)

    private static string GetImageUrlFromNode(string nodeTypeAlias, int nodeId)
    {
                if (nodeId > 0)
                {
                    string propertyName = string.Empty;

                    switch (nodeTypeAlias)
                    {
                        case "Top 5 Article":
                            return GetImageUrlFromTop5Item(nodeId);
                        case "Video Article":
                            return GetImageUrlForVideo(nodeId);
                            break;
                        default:
                            propertyName = "image";
                            break;
                    }

                    if(!string.IsNullOrEmpty(propertyName))
                        return UmbracoUtility.GetUmbracoMediaItem(propertyName, nodeId);
                }
                return string.Empty;
    }

    private static string GetImageUrlFromTop5Item(int nodeId)
    {
                XmlDocument xml = new XmlDocument();

                //The code fails here because top5Items is null
                xml.LoadXml(UmbracoUtility.GetUmbracoItem("top5Items",nodeId));

                XmlNode imageNode = xml.SelectSingleNode("/items/item/data[@alias='image'][0]");
                return UmbracoUtility.GetMediaFile(imageNode.InnerText);
    }

    public static String GetUmbracoItem(String alias, int? nodeId)
            {
                String value = String.Empty;
                if (!nodeId.HasValue)
                {
                    Hashtable ht = HttpContext.Current.Items["pageElements"] as Hashtable;
                    if (ht != null)
                    {
                        if (ht[alias] != null)
                        {
                            return ht[alias].ToString();
                        }
                    }
                    return umbraco.library.GetItem(alias);
                }
                else
                {
                    try
                    {
                        value = umbraco.library.GetItem((int)nodeId, alias);
                    }
                    catch
                    { }
                    return value;
                }
            }

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies