When working with hierarchical content types, you will need to get child content items for a specific parent. The following procedure describes how to achieve this using the dynamic module manager.
For more information about the sample code snippets used in the article, see Example: Retrieve and create child items.
Before getting children of an item, we recommend to check if the item has any children. You do this in the following way:
// Determines if an item has child items assigned to it.
hasChildren = dynamicModuleManager.HasChildItems(item);
After you call manager.HasChildItems(item), to increase performance, you can set the SystemHasChildItems property and use it instead of manager.HasChildItems(item) in the following way:
hasChildren = item.SystemHasChildItems;
To retrieve the child items of a parent item, you must perform one of the following:
// Gets a collection of all of the child items only one level under the parent.
// In this case this will get all of the albums for a specified artist but won't get the songs for these albums.
var johnLennonAlbums = dynamicModuleManager.GetChildItems(johnLennon);
// Some custom logic goes here.
var title = album.GetValue<
// Populates 'SystemChildItems' property for every content item that is part of the hierarchy with parent 'John Lennon' content item.
var song = child.SystemChildItems.FirstOrDefault();
Hierarchical content types are working with a Sitefinity CMS workflow. If you have not defined any workflow, Sitefinity CMS uses the default one. The items are represented in two versions – Master and Live. When you create a new content item, Sitefinity CMS first creates the master version. After you publish the item, Sitefinity CMS creates the live version, which is a copy of the master one. Only the live versions of items are visible on the frontend. When you want to modify an item, you change the master version. When you publish the changes, a new live version is created and displayed on the frontend.
Because of this the hierarchy is persisted only by the master items. When you perform operations such as getting all of the children of an item or setting a parent, you use the master versions. Use the following examples to work with different versions of content items:
// Retrieving the parent DynamicContent item using the child item.
DynamicContent parentMasterVersion = childMasterVersion.SystemParentItem;
DynamicContent parentLiveVersion = manager.GetDataItems(contenType).Where(i => i.OriginalContentId == parentMasterVersion.Id && i.Visible == true).SingleOrDefault();
// Populates the 'ChildItems' property for every content item that is part of the hierarchy with parent 'masterParentItem' content item.
// Gets a collection with all of the master child items IDs.
var masterChildItemIds = masterParentItem.ChildItems.Select(i => ((DynamicContent)i).Id);
// Filters through all of the items to get the live ones that are representation of the master items.
// The OriginalContentId property for the live items contains the ID of the corresponding master version of the item
// The Visible property is used to filter only the items that are visible in the frontend because some might be live versions but unpublished in the backend
var liveChildItems = dynamicModuleManager.GetDataItems(contentType).Where(i => masterChildItemIds.Contains(i.OriginalContentId) && i.Visible);
Back To Top