Finding content

N2's API provides a few options to help finding items stored in the system.

Getting the children of a page 

The most straight-forward would be iterating the page tree using the GetChildren method. An important distinction between the GetChildren() method and the Children collection is that GetChildren filters out items not accessible to the currently logged on user. It's a good idea to use GetChildren unless you really want all items regardless of permissions.

Writing level 1 item's titles

The GetChildren has a few overloads. E.g. you can pass a filter object to the method. The code below will output the titles of pages below the root page of the type NewsPage. If there are unpublished or secured pages these will also be outputted since the AccessFilter wasn't passed.

Getting children and filtering by type

Traversing child pages recursively 

The code until now didn't consider pages deeper in the page hierarchy. To find pages on deeper level you could create a recursive method. This code will return all news pages below the starting point accessible to the current user. 

Traversing page tree finding all items of a certain type

Querying for pages

Another option for finding items is the item finder API. The code below will return all news pages (regardless of location or security considerations). 

Finding all items of a certain type

With the finder it isn't as straight-forward to find items below a certain parent. One option would be using the ParentFilter.

Finding all items of a certain type below a certain page

Two approaches at finding items

Traverse the tree and find all news published within the latest month. Sort by published date. 

Traversing page tree to find news created the latest month

Same thing but using the finder. 

Querying the database for items created the latest month and filtering out those below a certain page

An example

Lets say we want to display a news bill on the start page. For this we've deviced a detail on the news page: IsNewsBill. We'll use this find the latest news bill to display.

Defining a boolean detail on the news page

And this is how we find it:

Defining a boolean detail on the news page