Requesting help to query content based on tags

Topics: Customizing Orchard
Mar 12, 2014 at 7:11 PM
Edited Mar 12, 2014 at 9:03 PM

I am using Orchard 1.7 and am requesting help to query content based on tags.


I am not successful in getting code below to work from another post a few years back.
// Get a list of content items with each of the tags and normalise the list
List<IContent> taggedContent = new List<IContent>();
foreach (string tag in tags)
    TagRecord tagRecord = _tags.GetTagByName(tag);
    if (tagRecord != null)
        // Here we could have used the skip/take overloads to only get max items
        // but that wouldn't give us the ability to order by published date... so 
        // until I work out how to use the query interface directly this will get
        // all content items that have one of our tags into a list and then
        // we sort and take from there.
        IEnumerable<IContent> matchesForThisTag = _tags.GetTaggedContentItems(tagRecord.Id, VersionOptions.Published);
        foreach (IContent item in matchesForThisTag)
            if (!taggedContent.Contains(item)) taggedContent.Add(item);

// We now have a unique list of IContent that matches our content
// Sort it, take the top N and then build the display for each 
// into our display list.
var list = shapeHelper.List();
list.AddRange( taggedContent
        .OrderByDescending(i => i.ContentItem.As<CommonPart>().PublishedUtc)
        .Select(p => _orchard.ContentManager.BuildDisplay(p, "Summary")));

return ContentShape("Parts_RelatedContentWidget",
    () => shapeHelper.Parts_RelatedContentWidget(
            ContentItems : list