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])));