brother
brother

Reputation: 8151

Find pages with tag

In a Umbraco 7 solution, i have a Tags Content picker on all pages. Pages can with this, set tags on each page.

I then want to get alle pages, within the intire site, that has, lets say tag 111 (id, not name).

I have tried with:

var ids = Model.MacroParameters["tags"]; //the tags to show
CurrentPage.AncestorOrSelf(1).Descendants().Where(x => ids.Contains(x.tags.ToString()));

But that gives me the error:

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

Whats the correct way?

Upvotes: 0

Views: 323

Answers (3)

Gerty
Gerty

Reputation: 146

Always try to avoid using Descendants, especially on the root node. To get the tags for a property:

ApplicationContext.Current.Services.TagService.GetTagsForProperty(Model.Content.Id, "propertyname")

To find content with a specific tag:

ApplicationContext.Current.Services.TagService.GetTaggedContentByTag("tag")

Upvotes: 0

elolos
elolos

Reputation: 4450

You have a few options, depending on whether you prefer a dynamic or strongly typed view model.

Strongly Typed API

Umbraco.TypedContentAtRoot().Descendants().Where(x => x.tags.Contains(ids));

Dynamic API

Umbraco.ContentAtRoot().Descendants().Where("tags.Contains(@0)", ids);

Please note that the Contains statement may give you inconsistent results, as the tags property seems to be returning a comma separated list. In that case you can try splitting the string or install the Core Property Value Converters package and get the tags as IEnumerable<IPublishedContent>

Upvotes: 1

brother
brother

Reputation: 8151

Solved it with;

Umbraco.Content(rootId).Descendants().Where("tags.Contains(@0)", ids);

Upvotes: 1

Related Questions