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?)
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?
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:
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:
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?
If you are just looking to get the root node, could you not just use the
TypedContentAtRoot()
of theUmbracoHelper
class as this returns an object of typeIEnumerable<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?You need to include the umbraco.web namespace :). That has caught me out so many times :')
Also in this
write it short hand :). And handle the exception if AncestorOrSelf is null :)
Thanks a lot Charles! using Umbraco.Web; I was going crazy trying to get Descendants of a IPublishedContent ;-)
Thnx Charles, import umbraco.web namespace was the solution.
is working on a reply...