0

I have an ArticlePage template with a TopicTags field, which is a treelist pointing to a list of tags. Users can select one or two tags. I have a requirement to search for two related articles that are also of the 'ArticlePage' type and have at least one matching tag from the current article page. I have the following code, but I couldn't make it work - looks like the TopicTags field ToString does not produce Guid1|Guid2.

thanks for your help!

BTW, this link does not work: adding-a-property-field-to-an-index

var indexName = String.Format("sitecore_{0}_index", Sitecore.Context.Database.Name);
ISearchIndex selectedIndex = ContentSearchManager.GetIndex(indexName);
using (var context = selectedIndex.CreateSearchContext())
{
  var currentPageTags = new List<string>(); 

  tags = (MultilistField)pageItem.Fields["TopicTags"];

  if (!string.IsNullOrEmpty(tags.Value)) {
    var tokens = tags.Value.Split(new char[] { '|' });
    foreach (var token in tokens) { 
       currentPageTags.Add(token);
    }
  }
  var searchFolder = Sitecore.Context.Database.GetItem("/sitecore/content/fedpartnership/home/pages");

  var searchResults = context.GetQueryable<SearchResultItem>()
      .Where(x => x.Paths.Contains(searchFolder.ID) 
         && string.Compare(x.TemplateName, "ArticlePage", true ) == 0 
         && x.Name != pageItem.Name
         && (x.Fields["TopicTags"].ToString().Contains(currentPageTags[0])
            || currentPageTags.Count == 2 && x.Fields["TopicTags"].ToString().Contains(currentPageTags[1])));

4
  • Check this stackoverflow.com/questions/2912476/… . You need a similar logic like, CurrentTagsArray = [guid1, guid2] and stringToMatch = "guid2|guid3" Commented Oct 5, 2023 at 7:00
  • Just few correction in your code - 1) You need NOT operator in 3rd line 2) Check spelling of template name in last line, it is 'ArcticlePage' now. Commented Oct 5, 2023 at 7:03
  • Please use the index for queries like this instead of searching through the Sitecore database...
    – Gatogordo
    Commented Oct 5, 2023 at 7:42
  • sitecore.stackexchange.com/questions/13351/… does not solve my problem
    – John
    Commented Oct 5, 2023 at 15:52

0

Browse other questions tagged or ask your own question.