WebAPI nodes
Overview
The content and folders in Contensis are represented in the WebAPI as Node objects. These Node objects are linked together as a tree in exactly the same way that you see the Project Explorer on the left-hand side in Contensis.
Standard Properties
The standard properties for any content or folder within Contensis are available as direct properties and methods of the Node class and it's inheriting classes (ContentNode and FolderNode). These properties include Title, Path, ID, etc and the full list of Node properties can be found here. There are also additional specific FolderNode properties and ContentNode properties.
The simple example below show how to display some properties of the current Node within a page:
<h1>@CurrentNode.Title</h1>
<p>The path for the current page is @CurrentNode.Path</p>
|
Will output the following example:
<h1>My Page</h1>
<p>The path for the current page is /Products/Books/</p>
|
Check the type of Node
Remember that a Node can either be a ContentNode or a FolderNode, which have different additional properties and methods on top of Node. Ensure that you check the type of the Node first to ensure you don't get any run-time errors, especially when using properties or methods that return collections of the base class Node.
Node navigation
There are a number of properties and methods for navigating the node tree.
Node (Inherited by ContentNode and FolderNode)
FolderNode Specific
- Children
- Descendants
- Where
- Find
Parent
This example navigates up 2 nodes to an ancestor FolderNode and then lists that FolderNode's children:
Source Code
@{
var childNodes = @CurrentNode.Parent.Parent.Children();
}
<ul>
@ foreach (var node in childNodes)
{
<li>@node.Title</li>
}
</ul>
|
This will produce something similar to the following:
<ul>
<li>Page 1</li>
<li>Page 2</li>
<li>Page 3</li>
...
</ul>
|
Siblings
Siblings are Nodes which are at the same level in the tree as the current Node. The examples below show how the Siblings method can be used to either return siblings by Node type and how to include/exclude the current node itself.
Source Code
var siblings = node.Siblings();
var siblings = node.Sibllings( true );
var siblings = node.Siblings<FolderNode>();
var siblings = node.Siblings<FolderNode>( true );
var siblings = node.Siblings<Node>();
var siblings = node.Siblings<Node>( true );
|
Children
Children are Nodes which are at the level below in the tree of the current Node. The examples below show how the Children methods can be used to return siblings by Node type and how to access child nodes by their index.
Source Code
FolderNode node = CurrentNode.Parent;
var first = node.Children()[0];
var folders = node.Children<FolderNode>();
var products = node.Children().Where(n => n.Title.Contains( "product" ));
|
Descendants
Descendants work in the same way as the Children methods except that the Nodes are selected from all levels below the current Node. The examples below show how the Children methods can be used to return siblings by Node type.
Source Code
FolderNode node = CurrentNode.Parent;
var nodes = node.Descendants();
var folders = node.Descendants<FolderNode>();
|
Where
The Where method is essentially a Linq-style shortcut for filtering descendants of a FolderNode. The example below shows how to get descendant content that has a specific metadata value set.
var productNodes = myFolderNode.Where(n=> n.Data.Isbn != null );
|