Peter
Peter

Reputation: 14508

Find pages with certain tags in Sitefinity

I'm looking for a way to find all pages that contain certain tags. For documents I can do it like this:

// convert list of tags to list of Guids
var tagGuids = GetGuidsForTags(tags);
// find all items with one of these tags
return App.WorkWith().Documents()
    .Where(ni => ni.GetValue<IList<Guid>>("Tags").Any(tag => tagGuids.Contains(tag)) &&
        ni.Status == ContentLifecycleStatus.Live
    )
    .Get().DistinctBy(x => x.Id).ToList();

But when using App.WorkWith().Pages() instead, I get an errormessage saying that a PageNode does not have a custom field named "Tags".

An exception of type 'System.Exception' occurred in Telerik.Sitefinity.Model.dll but was not handled in user code

Additional information: Wrong custom field 'Tags' for type 'Telerik.Sitefinity.Pages.Model.PageNode'

Does anyone know how to get a list of pages that contain certain Tags? I am using Sitefinity 8.1.

To clarify I didn't add a custom Tags field to my Pages. And when I do, I get the field twice: tags field showing twice

So Sitefinity seems to have a Tags field by default...

Upvotes: 0

Views: 417

Answers (2)

Peter
Peter

Reputation: 14508

Apparently I had a misconfigured Sitefinity. Once I removed the selfmade custom field Tags it removed the original one as well. So now I am left with only one, which is now a custom field. Once this was done, I could access the tags using the code in my original question...

Wish I could explain it as it took me quite a while to figure this out.

Upvotes: 0

Jon R.
Jon R.

Reputation: 999

I am going to assume you added a custom page fields called "Tags".

Custom Page Field "Tags" If you have the field this code will get you the pages.

    var pages = App.WorkWith().Pages()
                .LocatedIn(PageLocation.Frontend)
                .ThatArePublished()
                .Where(p => p.GetValue<TrackedList<Guid>>("Tags").Any(tag => tagGuids.Contains(tag)))
                .Get().ToList(); //ToList to commit to memory for iteration if needed

Upvotes: 1

Related Questions