Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I have one document type that has two different custom tag fields. What is the most efficient way to create a single search request that can query both fields at once? Should I try using TagQuery or go a more traditional route?
EDIT: by "both fields at once", I mean one search field for each field, so I can search for docs with the value "a" in tag group 1 and "b" in tag group 2.
You could use TagQuery...
var helper = new UmbracoHelper(UmbracoContext.Current);
var tags1 = helper.TagQuery.GetTagsForEntity(CurrentPage.Id, "TagGroup1NameHere");
var tags2 = helper.TagQuery.GetTagsForEntity(CurrentPage.Id, "TagGroup2NameHere");
List<Umbraco.Web.Models.TagModel> combinedResults = new List<Umbraco.Web.Models.TagModel>();
Then you can do what you want with combinedResults (i.e. Loop over them OR use it as a data source for any data controls like the Repeater etc. - WebForms).
Couple things, though - GetTagsForEntity seems like it pulls the tags for a single node. What I'm looking for is a search of all children node (of a certain docType). The combinedResults approach seems right, but I think what I need is Umbraco.TagQuery.GetContentByTag(tag, "TagGroup1NameHere").
I didn't realize the groupName was a potential second parameter; thanks for pointing me in the right direction!
Also, that addRange will create a superset of tags1 and tags2, while I need the intersection. What is the most effective way to pull the records common to both lists?
Yes, you are correct. .GetContentByTag is what you would want in your case.
I would say the most effective way would be to loop over all results
and convert them to a list of strongly typed objects (i.e. IPublishedContent OR you could create your own DTO) as the TagModel will only return you the Id of each node that it matched.
IList<IPublishedContent> taggedNodes = new List<IPublishedContent>();
foreach (var c in combinedResults)
var matchedNode = helper.TypedContent(c.Id);
if (matchedNode != null)
You can then do what you wish with the strongly typed list taggedNodes
Hope this helps ;)
Write your reply to:
Image will be uploaded when post is submitted