To give you a code example which ancestor are you after as nodes of a certain document type can be scattered though-out the tree ? The uQuery helper library in uComponents may be useful as there are methods to get NodesByType() and GetNodesByXPath(), as well as being able to iterate though the tree with XPath axis type extension methods on the Node and Media objects.
Access first ancestor node of certain doc type
I know how to get the first ancestor node of a certain document type in XSLT, how do yoiu achieve the same thing in C# code?
Hi Brendan,
To give you a code example which ancestor are you after as nodes of a certain document type can be scattered though-out the tree ? The uQuery helper library in uComponents may be useful as there are methods to get NodesByType() and GetNodesByXPath(), as well as being able to iterate though the tree with XPath axis type extension methods on the Node and Media objects.
HTH,
Hendy
Hi Brendan,
+1 for the above. Here is some code you might find useful (out of the top of my head, so there might be some syntax errors...):
var currentNode = Node.GetCurrent();
var myDocumentType = "ancestor doc type to look for";
var level = 2;
var ancestorId = GetAncestor(currentNode, myDocumentType, level);
private int GetAncestor(Node currentNode, string myDocumentType = "", int level = -1){
if (currentNode != null) {
if ((string.IsNullOrEmpty(myDocumentType) || currentNode.NodeTypeAlias == myDocumentType) &&
(level < 0 || currentNode.Level == level)){
return currentNode.Id;
} else if (currentNode.Parent != null) {
Node parent = new Node(currentNode.Parent.Id);
return GetAncestor(parent, myDocumentType, level);
}
}
Hope that makes sense and helps,
Sascha
Hi,
I've not tried either yet, but I wonder if this is the equivalent using Linq and uQuery ?
Cheers,
Hendy
is working on a reply...