Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

Kentico: retrieving related documents

Modified: 2013/09/24 17:38 by vanthoog - Categorized as: Kentico
The following applies to Kentico v7.0. It probably also applies to previous versions, although this has not been tested.

In Kentico it is possible to define a n-m relation between documents by using a relationship. Now suppose you are creating a custom webpart and you want to retrieve the related documents of a specific document via the api. How can you do this? This can be achieved by using CMS.CMSHelper.TreeHelper.SelectNodes. The difficulty with this method is that there are many overloads with many different parameters. The overload to use is:

// Summary:
//     Selects tree nodes without coupled data according to provided parameters
//     and returns them as dataset.
//
// Parameters:
//   aliasPath:
//     Path. It may contain % and _ as wild card characters for any number of unknown
//     characters or one unknown character respectively (for MS SQL)
//
//   combineWithDefaultCulture:
//     Specifies if return the default culture document when specified culture not
//     found
//
//   classNames:
//     List of classNames to select separated by semicolon (e.g.: "cms.article;cms.product")
//
//   where:
//     Where condition to use for the data selection
//
//   orderBy:
//     Order by clause to use for the data selection
//
//   maxRelativeLevel:
//     Maximal child level of the selected nodes
//
//   selectOnlyPublished:
//     Select only published nodes
//
//   relationshipWithNodeGuid:
//     Select nodes that are related to the Node GUID
//
//   relationshipName:
//     Relationship name
//
//   relatedNodeIsOnTheLeftSide:
//     Specify if the related node is located on the left side of the relationship
public static TreeNodeDataSet SelectNodes(string aliasPath, bool combineWithDefaultCulture, string classNames, string where, string orderBy, int maxRelativeLevel, bool selectOnlyPublished, Guid relationshipWithNodeGuid, string relationshipName, bool relatedNodeIsOnTheLeftSide);

Unfortunately this overload has many parameters but fortunately most of them can left empty. The ony required parameters are:
- relationshipWithNodeGuid:
- relationshipName:
- relatedNodeIsOnTheLeftSide:

Here is an example:

// Retrieve all related items based on the specified relationship.
TreeNodeDataSet treeNodes = TreeHelper.SelectNodes(
	null,
	true,
	"", // No restriction on class name (i.e. document type).
	"", // No where condition.
	null,  // No order by clause.
	-1, // No level restriction.
	true,
	contentItem.NodeGUID, // Guid of the content item.
	relationShipClassName, // Name of the relationship.
	false // Parent node is on the right side of the relation.
	);

 © Evident Interactive BV