Retrieving contents

Created by Webnodes

There are several methods you can use to retrieve content objects.

Retrieving contents content directly from the WAF Session object

 

To retrieve content one by one you can the method “.GetContent()” on the WAFSession object and pass the NodeId:

var article = WAFContext.Session.GetContent<ArticleBase>(1000);

 

The method has several overloads, and you can call it using an AQL query:

var article = WAFContext.Session.GetContent<ArticleBase>(AqlArticleBase.Name == "Test");

 

This example would retrieve the article named “Test”. The method throws an exception if the query results in 0 or more than one contents.

To get the content associated with the current request you can call the “.GetContent()” method on the WAFRequest object:

var article = WAFContext.Request.GetContent<ArticleBase>();

 

Internally this uses the session object associated with the page request.

To get the User associated with the current request you can call this:

if (WAFContext.Session.Access.IsStored) 
{
   var user = WAFContext.Session.GetUser();
}

 

Or the eqiuvalent using the standard GetContent method:

 if (WAFContext.Session.Access.IsStored) 
{
   var user = WAFContext.Session.GetContent<SystemUser>(WAFContext.Session.UserId);
}

 

To retrieve multiple contents you can call the “.GetContents()” on the WAFSession object.

This method has several overloads. The simplest is to base the query on the type of object, for example, to retrieve all system users’s you can call this:

var users = WAFContext.Session.GetContents<SystemUser>();

 

Please note that this call also will also return all content objects that inherit from the SystemUser class.

The method supports AQL query expressions. Example:

var users = WAFContext.Session.GetContents<SystemUser>(AqlSystemUser.Birth > new DateTime(1980, 1, 1));

 

This returns all users born after 1980. There are several other overloads on this method where you can set paging and sorting. Please see the API reference for more information. Here is another example:

var users = WAFContext.Session.GetContents<SystemUser>(AqlSystemUser.Birth > new DateTime(1980, 1, 1), AqlSystemUser.Forename, 10);

 

This call will sort the result on the user’s forename and only retrieve a maximum of 10 users.

Retrieving contents from other contents

 

Contents can be retrieved through relation properties. Here is an example using the “.Children” property of the HierarchalContent object:

var article = WAFContext.Request.GetContent<ArticleBase>();
foreach (var child in article.Children.Get()) {          

}

 

The “.Children” property is a Node Relations Property part of a One-to-Many relation. Internally the “.Get()” method uses the session first used to retrieve the “article” to make an AQL join query on the relation. That means the return list of contents will be filtered for contents the current session does not have read access to. Each of the children will also be associated with the same session so any call on relation properties on the children will also be filtered.

Below is another example using the “.Parent” property:

var article = WAFContext.Request.GetContent<ArticleBase>();
if (article.Parent.IsSet()) 
{
   var parent = article.Parent.Get();
}

Retrieving contents from through AQL queries

 

Contents can also be retrieved through AQL queries. In AQL queries you can perform complicated content queries in a structured manner. The full topic of AQL is covered later in this manual. Below is a basic example:

var q = WAFContext.Session.CreateQuery();
var rUser = q.Select<SystemUser>();
SystemUser u;
q.Where(AqlSystemUser.Gender == (int)Gender.Male);
q.Where(AqlSystemUser.City == "London" | AqlSystemUser.City == "Oslo");
q.OrderBy(AqlSystemUser.Birth);
var users = q.Execute<SystemUser>();

 

This query returns all male users that live in London or Oslo. The result is sorted by birth date.