Copied to clipboard

Flag this post as spam?

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


  • firepol 125 posts 173 karma points
    Jun 03, 2014 @ 10:57
    firepol
    0

    IPublishedContent extension methods not available in c#

    Hi, I regularly use in my razor views the extension methods listed in the Querying & Traversal documentation, such e.g. var root = Model.Content.AncestorOrSelf(1);

    However, now I'm trying to build a more complex function to retrieve the same page located under another language, for a multi-language website. I can do that in the razor view, but I'd like to create a static helper method, instead, so that I can reuse it in different views, if needed.

    It looks like this:

            public static List GetPageURLinAllLanguages(IPublishedContent content)
            {
                List links = new List();
    
                return links;
            }
    

    As you can see, I passed the IPublishedContent. If I try to get the root node from this content, as I do in my views, like this:

            public static List GetPageURLinAllLanguages(IPublishedContent content)
            {
                List links = new List();
    
                var root = content.AncestorOrSelf(1);
    
                return links;
            }
    

    I get AncestorOrSelf(1) underlined in red, and if I compile I get this error:

    Error 6 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'AncestorOrSelf' and no extension method 'AncestorOrSelf' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)

    What am I missing? Can you help?

  • Tim Anderson 20 posts 83 karma points
    Jun 06, 2014 @ 17:35
    Tim Anderson
    0

    If you are just looking to get the root node, could you not just use the TypedContentAtRoot() of the UmbracoHelper class as this returns an object of type IEnumerable<IPublishedContent>.

    If you only have 1 piece of content at the root, you could just call .First() on that result or if not enumerate through the result to find the correct piece of content at the root of your content tree?

  • Charles Afford 1163 posts 1709 karma points
    Jun 08, 2014 @ 12:07
    Charles Afford
    1

    You need to include the umbraco.web namespace :).  That has caught me out so many times :')

  • Charles Afford 1163 posts 1709 karma points
    Jun 08, 2014 @ 12:15
    Charles Afford
    1

    Also in this

    List links =newList();

               
    var root = content.AncestorOrSelf(1);

               
    return links;
    return content.AncestorOrSelf(1) != null ? content.AncestorOrSelf(1) : null;
    write it short hand :). And handle the exception if AncestorOrSelf is null :)
  • Ivan 139 posts 302 karma points
    Mar 17, 2015 @ 14:04
    Ivan
    0

    Thanks a lot Charles! using Umbraco.Web; I was going crazy trying to get Descendants of a IPublishedContent ;-)

  • David 3 posts 21 karma points
    May 20, 2015 @ 16:15
    David
    0

    Thnx Charles, import umbraco.web namespace was the solution.

Please Sign in or register to post replies

Write your reply to:

Draft