Copied to clipboard

Flag this post as spam?

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


  • Mark 7 posts 28 karma points
    May 24, 2014 @ 22:22
    Mark
    0

    Using a class from the app_code folder not working.

    Hello,

    I've created a class in the app_code section of my website and instantiated this class into my macro, however it doesn't seem to work. I tested the linq and functionality in a winform application and seems to work in the way i want to... just doesn't seem to render using the macro, here is what i have:

    Macro:

    @using umbraco.MacroEngines
    @using umbraco.MacroEngines.Library;

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
        DynamicNodeList pages = @Model.Children.Where("Visible").OrderBy("CreateDate desc");
     }

    @{
        Common com = new Common();

        List<string> tags = new List<string>();
       
        string[] top10 = @com.GetTop10Tags(@pages);
       
        foreach(string tag in @top10)
        {
            <ul>@tag</ul>
        }
    }

    App_Code - Common Class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using umbraco.MacroEngines;
    using umbraco.MacroEngines.Library;

    public class Common
    {
        public string[] GetTop10Tags(DynamicNodeList pages)
        {
            List<string> tags = new List<string>();

            foreach (var node in pages)
            {
                string[] aTags = node.GetPropertyValue("postTags").Split(',');

                foreach (string tag in aTags)
                {
                    tags.Add(tag);
                }
            }

            string[] orderedList = tags
                .GroupBy(i => i)
                .OrderByDescending(g => g.Count())
                .Select(g => g.Key).ToArray();

            return orderedList;
        }
    }

    Does anyone see what the problem here is?
    Error: Error loading MacroEngine script (file: TagCloud.cshtml)

  • Mark 7 posts 28 karma points
    May 25, 2014 @ 00:17
    Mark
    0

    public string[] GetTop10Tags(DynamicNodeList pages)
        {
            List<string> tags = new List<string>();

            foreach (DynamicNode node in pages)
            {
                if (node.GetPropertyValue("postTags") != null)
                {
                    string[] aTags = node.GetPropertyValue("postTags").Split(',');

                    foreach (string tag in aTags)
                    {
                        tags.Add(tag);
                    }
                }
            }

            string[] orderedList = tags
                .GroupBy(i => i)
                .OrderByDescending(g => g.Count())
                .Select(g => g.Key).Take(10).ToArray();

            return orderedList;
        }

    I got this working by using the above code, not sure why this is the case but by switching the var to DynamicNode helped, as well as adding validation after the foreach to check if the node contains any tags within the tag property... :)

Please Sign in or register to post replies

Write your reply to:

Draft