I'm currently developing a new website powered by Umbraco 7.6.1 and I'm having some trouble porting some code that was running under 7.4.3
Previously I used the following code to get a list of all descendants from the current node that had a specific tag:
var tag = Request.QueryString["tag"] ?? "";
var nodes = CurrentPage.Descendants("page").Where("string.IsNullOrEmpty(@0) || tags.Contains(@0)", tag).OrderBy("CreateDate DESC");
However running this code under 7.6.1 yields no results, but if I use:
var taggedPages = Umbraco.TagQuery.GetContentByTag(tag);
I get the correct results but I cannot see how I can limit the results to descendants of a specific node nor how I can implement paging.
However if you are talking a few (100's not 1000s of nodes) you might want to use the TagQuery - which should be quicker and you can do the following to just get the tags that are below the page you are on.
var taggedPages = Umbraco.TagQuery.GetContentByTag(tag)
.Where(x => x.Path.StartsWith(Model.Content.Path));
and while this isn't ideal. you can do some pageing with Skip() and Take().
var pagedContent = taggedPages.Skip(pageNo-1).Take(pageSize)
this should be ok for a few pages (approx 100 nodes say) but it might slow things down if you have 1000s of pages tagged across your site.
Descendants by Tag
I'm currently developing a new website powered by Umbraco 7.6.1 and I'm having some trouble porting some code that was running under 7.4.3
Previously I used the following code to get a list of all descendants from the current node that had a specific tag:
However running this code under 7.6.1 yields no results, but if I use:
I get the correct results but I cannot see how I can limit the results to descendants of a specific node nor how I can implement paging.
Any ideas?
Hi
under 7.6.x depending on other settings using dynamic types doesn't work .
the Typed Version of what you want is.
However the current recommendation is try to avoid Descendants calls - so there is probably some clever XPath that can do this quicker.
However if you are talking a few (100's not 1000s of nodes) you might want to use the TagQuery - which should be quicker and you can do the following to just get the tags that are below the page you are on.
and while this isn't ideal. you can do some pageing with Skip() and Take().
this should be ok for a few pages (approx 100 nodes say) but it might slow things down if you have 1000s of pages tagged across your site.
That's brilliant, thanks for your help, I'll have a play around with your suggestions tomorrow.
is working on a reply...