Razor: How to get ancestor where boolean set to true in Umbraco 7
Hi there
I'm working on a Umbraco 7 solution. On my pages the editors can select some related pages using a MultiNodeTree Picker. Theese are renderes using a helper in my Razor partial, and everything is fine. That code look like the following:
@renderSharedContent(CurrentPage.selectedPages)
...
...
...
@helper renderSharedContent(String listOfIds)
{
var boxList = listOfIds.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
// Convert box list to Typed Content
var boxCollection = Umbraco.Content(boxList);
// Looping through selected shared content items as IPublishedContent (Typed Content)
foreach (IPublishedContent item in boxCollection)
{
//Calling partial, not cached
@Html.Partial(@item.DocumentTypeAlias, item)
}
}
So what I need to implement now is the ability to inherit the selected pages to the underlying pages.
This means that on each page I have a true/false property called "Inherit selected pages". If this checkbox is set to true the selected pages must be shown on the underlying pages.
On Section 1 I have selected two pages and checked the "Inherit selected pages". This means that when I'm standing on any of the pages below this page they should render the selected pages from the ancestor page.
It's possible to both show inherited pages, and also render pages selected on the single page. So if I've selected some additional pages on the page "SubSubpage 1.1.2", the page would both show the inherited pages and the "local" selected pages below.
But I can't find out how to find the ancestor node that have the checkbox ("inheritSelectedPages") set to true, and then use the content from that node in the "selectedPages"-MNTP.
I guess that if I had the ancestor, I could render my pages like the following:
var inheritNode = [what to write here??]
@renderSharedContent(inheritNode.selectedPages)
@renderSharedContent(CurrentPage.selectedPages)
Btw. if both the parent, the parents parent etc. have checked the "inheritSelectedBoxes" it should only be the pages from the parent that should be inherited. The first node with the "inheritSelectedPages" in the "way up" the nodes.
I hope that my question makes sense, and hat someone could give me some pointers on how to achieve this :)
Sorry for my stupidity, but I'm not quite sure how I use your snippet. I come from a XSLT background (I can't write C#) so in XSLT I would just write something like:
$currentPage/ancestor-or-self::*[@isDoc and inheritSelectedPages='1' and @id!=$currentPage/@id]
Something like the above would give me the node of the closest ancestor with the true/false-property checked.
I'm not sure how I could achieve this with the above code you provided me.
There's not a way to achieve something like that, so I could use it like below?
var inheritNode = [what to write here??]
@renderSharedContent(inheritNode.selectedPages)
@renderSharedContent(CurrentPage.selectedPages) //This line already works perfectly
Maybe I'm just dreaming of doing this "the easy way" and to grab the node into the inheritNode variable.
But if that's not possible how should I implement your code in my Partial?
Razor: How to get ancestor where boolean set to true in Umbraco 7
Hi there
I'm working on a Umbraco 7 solution. On my pages the editors can select some related pages using a MultiNodeTree Picker. Theese are renderes using a helper in my Razor partial, and everything is fine. That code look like the following:
So what I need to implement now is the ability to inherit the selected pages to the underlying pages.
This means that on each page I have a true/false property called "Inherit selected pages". If this checkbox is set to true the selected pages must be shown on the underlying pages.
So let's say that I have the following content.
On Section 1 I have selected two pages and checked the "Inherit selected pages". This means that when I'm standing on any of the pages below this page they should render the selected pages from the ancestor page.
It's possible to both show inherited pages, and also render pages selected on the single page. So if I've selected some additional pages on the page "SubSubpage 1.1.2", the page would both show the inherited pages and the "local" selected pages below.
But I can't find out how to find the ancestor node that have the checkbox ("inheritSelectedPages") set to true, and then use the content from that node in the "selectedPages"-MNTP. I guess that if I had the ancestor, I could render my pages like the following:
Btw. if both the parent, the parents parent etc. have checked the "inheritSelectedBoxes" it should only be the pages from the parent that should be inherited. The first node with the "inheritSelectedPages" in the "way up" the nodes.
I hope that my question makes sense, and hat someone could give me some pointers on how to achieve this :)
Thank you very much in advance.
/Kim A
Hi Kim,
To get ancestor you can use the following methods:
http://our.umbraco.org/documentation/Reference/Mvc/querying
Hi Oleg
Thanks four your answer.
I'm aware of the AncestorsOrSelf.
I forgot to mention in my first post, that I have tried some different suff to achieve this. For example I have tried something like the below:
But the above seems to give me a true/false in return, so it's not what I'm after.
Any other ideas are welcome :)
/Kim A
Hi Kim,
If I correct understand your issue the following may help:
public IPublishedContent GetInheritedAncestor(IPublishedContent content)
{
if (content.Parent != null)
{
var isInherited = content.Parent.GetPropertyValue("inheritSelectedPages");
if (isInherited)
{
return GetInheritedAncestor(content);
}
return content.Parent;
}
return null;
}
Thanks,
Oleg
Or if you need all parents with inherited property = true
Oleg
Hi again Oleg
Sorry for my stupidity, but I'm not quite sure how I use your snippet. I come from a XSLT background (I can't write C#) so in XSLT I would just write something like:
Something like the above would give me the node of the closest ancestor with the true/false-property checked.
I'm not sure how I could achieve this with the above code you provided me.
There's not a way to achieve something like that, so I could use it like below?
Maybe I'm just dreaming of doing this "the easy way" and to grab the node into the inheritNode variable.
But if that's not possible how should I implement your code in my Partial?
Maybe it's some stupid question, sorry :-/
/Kim A
is working on a reply...