Sitefinity CMS provides you with an API for managing related items using the RelatedDataExtensions public class. The API consists of extension methods over object items (facilitates the use of the API in widget templates) and over IDataItem for creating and deleting item relations.
IMPORTANT: When using with the related data API, you need to work with the master versions of both the related data item and the item, to which you are creating a relation.
You can create content item relations using the following methods:
NOTE: If there is no field with such a name, a relation is still created, even though it will not visible in Sitefinity backend. You can only access the relation via the Related data API, using the same fieldName. The above-described is generally the case when creating relations to content types not supported by the Related data field.
You can delete item relations using the following methods:
The following code demonstrates how to create item relations:
var providerName = String.Empty;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
Type locationType = TypeResolutionService.ResolveType(
var location = dynamicModuleManager.GetDataItems(locationType) .FirstOrDefault(t=>t.Status == ContentLifecycleStatus.Master)
Type sessionType = TypeResolutionService.ResolveType(
var session = dynamicModuleManager.GetDataItems(sessionType) .FirstOrDefault(t=>t.Status == ContentLifecycleStatus.Master);
&& session !=
// create a relation to location. Relation will be available only for the current session item state (Master)
// on publish, all item's relations are copied from Master to Live availability.
In addition, you can delete the relation between the session and the location you created above using the following code:
// delete the relation between the session item and the location
session.DeleteRelation (location, “Location”);
You can retrieve all related child items of particular field using the methods, listed below.
NOTE:You need to enter the accurate corresponding related field name, otherwise the returned result is null.
You can retrieve all parent items of particular item using the methods, listed below.
NOTE:When using the IQueryable interface, be aware that content items do not have a Provider property set, so related data is not returned. The reason is that the Provider property is a complex object and it is not persisted in the database. It is only built when you load the query from the database. When you work with the IQueryable item from the collection, the Provider property is still null.
When the final collection is queried, you can use an IEnumerable interface to get single items from the collection.
NOTE: The returned result contains a list with all related data items. The data items are in the same status as the related item, whose child or a parent they are.
With the Related data API, you can optimize performance when retrieving related data items for a specific collection of items. You do this by setting this collection to the context and thus, after the first related item is requested, Sitefinity CMS loads all the related data items per property for this collection. After this initial request, each time there is an additional request from the API for the related data items, Sitefinity CMS returns the results for the items from the context and not from the provider.
To do this, use the following template:
<%@ Control Language=
<%@ Import Namespace=
<%--Shows the Session Title field--%>
<%--Shows the Session's Related Speaker Title field--%>
In the following example, you have a content type Sessions and you want to retrieve all Speaker related data items for this content type. In the example, the collection retrieves the Speaker related data items per the Session title field as soon as the first Session item from the collection requests its related data items.
The following code snippet demonstrates how to use the template for retrieving the related data items for the Sessions example, described above:
MySessions : System.Web.UI.UserControl
Repeater repeater =
var manager = DynamicModuleManager.GetManager();
Type sessionsType = TypeResolutionService.ResolveType(SessionsType);
// Get all published sessions
var items = manager.GetDataItems(sessionsType)
.Where(item => item.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && item.Visible);
// Set the items collection as related data source to the context of the current request
// Once the <asp:Repeater runat="server" ID="SessionsRepeater"> is called
// for the first session, with one call all the speakers for all the sessions will be retrieved and populated
// to the current request context.
// Bind the Repeater
repeater.DataSource = items;
/// The type of the Dynamic Content items to be retrieved
For a detailed tutorial about utilizing the Related data custom field, see Tutorial: Create DevReach site with related data feature.
Back To Top