Skip to main content
Logo

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)

  • Parent
  • Siblings 
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

// Get the ContentNode siblings excluding the current Node:
var siblings = node.Siblings();
 
// Get the ContentNode siblings including the current Node:
var siblings = node.Sibllings(true);
 
// Get the FolderNode siblings excluding the current Node:
var siblings = node.Siblings<FolderNode>();
 
// Get the FolderNode siblings including the current Node (if the node is type FolderNode):
var siblings = node.Siblings<FolderNode>(true);
 
// Get all Node siblings excluding the current Node:
var siblings = node.Siblings<Node>();
 
// Get all Node siblings including the current 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

// Get the parent of the current Node, which will be a FolderNode:
FolderNode node = CurrentNode.Parent;
 
// Get the first child node that is a content node - ContentNodes are returned by default:
var first = node.Children()[0];
 
// Get child nodes that are FolderNode instances:
var folders = node.Children<FolderNode>();
 
// Using Linq to filter the results:
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

// Get the parent of the current Node, which will be a FolderNode:
FolderNode node = CurrentNode.Parent;
 
// Get all descendant nodes
var nodes = node.Descendants();
 
// Get descendant nodes that are FolderNode instances:
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.

// Get all nodes that have a metadata item of Isbn
var productNodes = myFolderNode.Where(n=> n.Data.Isbn != null);