This article describes how to get and set parent items in a hierarchy of content types. For more information about the sample code snippets used in the article, see Example: Get and set parent items.
Use the following code to create a master version of an item:
DynamicContent artistItem = manager.CreateDataItem(musicArtistType);
Use the following code to set item's properties and publish it:
// Publishing the music artist.
ILifecycleDataItem publishedArtistItem = manager.Lifecycle.Publish(artistItem);
The item is published in Sitefinity CMS and its live version is created. When you call manager.SaveChanges() , the changes are persisted in the database. For more information about Sitefinity CMS lifecycle, see For developers: Content lifecycle.
After you create an artist, you can define an album for it. You achieve this by creating an album and publishing it through the lifecycle. You set the parent of the album, using the following code:
// To set a parent for an item pass the ID of the 'Master' version of the parent item.
The extension method SetParent(parentId, contentTypeName) assigns an album to an artist. You must pass the ID of the master parent item as a parameter. Do not leave child items without assigned them a parent, because they are persisted in the database, but are not visible. These items are only visible, if you remove the parent of their content type.
In addition to setting a parent for an item, use the following methods to work with hierarchical items:
// Retrieving a music artist by its album.
Guid artistId = albumItem.SystemParentId;
DynamicContent singer = dynamicModuleManager.GetDataItem(musicArtistType, artistId);
// Retrieving the parent content type name.
singerContentTypeName = albumItem.SystemParentType;
// Retrieving the parent DynamicContent item using the child item.
DynamicContent artist = albumItem.SystemParentItem;
// Determines if an item has child items assigned to it.
hasChildren = dynamicModuleManager.HasChildItems(johnLennon);
hasChildren = johnLennon.SystemHasChildItems;
Back To Top