Get and set parent items

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 CreateDataItem method to create a master version of an item. Then, you set its properties and publish it.

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.

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:

  • Get the parent item using the SystemParentId property for its child item.
    You must use the master version of the albumItem , then use method manager.GetDataItem() to return the master version of the parent. You can do this before and after you call manager.SaveChanges().
  • Retrieve the parent content type name.
    You can do this before and after you call manager.SaveChanges().
  • Get the parent content item using the child item.
    You can do this only after you call manager.SaveChanges() for all of the required content items.
  • Check whether a parent item has children.
    After you call manager.HasChildItems(item), the SystemHasChildItems property is set and you can be use it instead manager.HasChildItems(item) for better performance.

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?