1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Filtering by multiple tags

Filtering by multiple tags

3 posts, 0 answered
  1. Tom
    Tom avatar
    2 posts
    Registered:
    24 Feb 2016
    04 Jan
    Link to this post

    I created a custom module with an MVC widget that I'm trying to filter the content by tags.  How do I filter by multiple tags?

    This works for a single tag:

    var myCollection = dmm.GetDataItems(iconType).Where(p => (p.Status == ContentLifecycleStatus.Live && p.Visible == true)).Where("Tags.Contains((" + SelectedTags[0] + "))")

     

    I tried a comma delimited string of SelectedTags but that did not work.  

     

    Thanks

  2. Victor Leontyev
    Victor Leontyev avatar
    65 posts
    Registered:
    01 Jul 2014
    05 Jan in reply to Tom
    Link to this post

    Hi Tom, based on this documentation http://docs.sitefinity.com/filter-expressions-for-content-items

    You should use something like that: 

    ((Category.Contains("{Taxonomy ID}") OR Category.Contains("{Taxonomy ID}"))
  3. Tom
    Tom avatar
    2 posts
    Registered:
    24 Feb 2016
    05 Jan in reply to Victor Leontyev
    Link to this post

    Thanks Victor, that isn't the exact syntax, but after much trial and error, I did find a magic combination using dynamic linq.

    var Tags = "Visible = true AND Status = Live AND (";

         foreach (Guid Tag in selectedTags)      

            {        

                 Tags += "Tags.Contains((" + Tag + ")) OR ";      

           }                  

    var myCollection = dmm.GetDataItems(iconType).Where(Tags.Substring(0, Tags.Length-4) + ")")

    Couple notes...2 sets of parentheses are required for each Tags.Contains.  Tags cannot be a string value, it must be a var with guid value appended to it.  

     

    edit: changing my answer after finding a solution.

3 posts, 0 answered