For developers: Related data API
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.
NOTE: 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:
- CreateRelation(this IDataItem item, IDataItem relatedItem, string fieldName)
This method creates a relation between an item and a relatedItem by field name. The item context must have a related data field with the name fieldName.
- CreateRelation(this IDataItem item, Guid relatedItemId, string relatedItemProviderName, string relatedItemType, string fieldName)
This method creates a relation between an item and a related item by field name. A related item is described with the following properties: relatedItemId, relatedItemProviderName, relatedItemType.
You can delete item relations using the following methods:
- DeleteRelation(this IDataItem item, IDataItem relatedItem, string fieldName)
This method deletes a relation between an item and a relatedItem by field name. The item context must have a related data field with the name fieldName.
- DeleteRelation(this IDataItem item, Guid relatedItemId, string relatedItemProviderName, string relatedItemType, string fieldName)
This method will delete a relation between item and related item by field name. Related item is described with the following properties: relatedItemId, relatedItemProviderName, relatedItemType.
- DeleteRelations(this IDataItem item)
Deletes all relations for the current item. Both parent and child relations are removed, regardless of field names.
- DeleteRelations(this IDataItem item, string fieldName)
Deletes all child item relations from the particular field.
The following code demonstrates how to create item relations:
var providerName = String.Empty;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
Type locationType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.DevReach.Location"
);
var location = dynamicModuleManager.GetDataItems(locationType) .FirstOrDefault(t=>t.Status == ContentLifecycleStatus.Master)
Type sessionType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.DevReach.Session"
);
var session = dynamicModuleManager.GetDataItems(sessionType) .FirstOrDefault(t=>t.Status == ContentLifecycleStatus.Master);
if
(location !=
null
&& session !=
null
) {
// create a relation to location. Relation will be available only for the current session item state (Master)
session.CreateRelation(location, “Location”);
dynamicModuleManager.SaveChanges();
// on publish, all item's relations are copied from Master to Live availability.
dynamicModuleManager.Lifecycle.Publish(session);
dynamicModuleManager.SaveChanges();
}
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”);
dynamicModuleManager.SaveChanges();
You can retrieve all related child items of particular field using the following methods:
- IQueryable<IDataItem> GetRelatedItems(this object item, string fieldName)
Gets child related items by field name. The method returns a query with child data items as IDataItem.
- IQueryable<T> GetRelatedItems<T>(this object item, string fieldName) where T : IDataItem
Generic method for getting child related items by field name. It will return a query with child data items of type T.
- int GetRelatedItemsCountByField(this object item, string fieldName = null)
Gets the number of related child items. Filtering by field name can be applied.
- int GetRelatedItemsCountByType(this object item, string typeName = null)
Gets the number of related child items. Filtering by type name can be applied.
- IEnumerable GetItemsWithSameTaxons(this object item, string itemTaxonomyFieldName, string relatedItemsTypeFullName, int skip = 0, int take = 10)
Returns items from a specified type with at least one matching value for the specified taxonomy field name.
- IEnumerable GetItemsWithSameTaxons(this object item, string itemTaxonomyFieldName, string relatedItemsTypeFullName, string relatedItemsTaxonomyFieldName = null, string relatedItemsProviderName = null, int skip = 0, int take = 10, string additionalFilterExpression = null, string orderExpression = null)
Returns items from a specified type with at least one matching value for the specified taxonomy field name. Allows additional filtering to be applied
All parent items, of particular item, can be retrieved via the following methods:
- IQueryable<IDataItem> GetRelatedParentItems(this object item, string parentItemsTypeName, string parentItemProviderName = null, string fieldName = null)
Gets parent related items by parent type. The method returns a query with parent data items as IDataItem. Filtering by parent item field name can be applied. In this case, field name is the name of the related field linking to this item in the parent item.
- IQueryable<T> GetRelatedParentItems<T>(this object item, string parentItemProviderName = null, string fieldName = null) where T : IDataItem
Gets parent related items by parent type T. The method returns a query with parent data items of type T. Filtering by parent item field name can be applied. In this case, field name is the name of the related field linking to this item, in the parent item.
- IList GetRelatedParentItemsList(this object item, string parentItemsTypeName, string parentItemProviderName = null, string fieldName = null)
Gets a list of all parent items of a particular item from a specific type. You can use this method when working with the widget templates.
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.
Improvinged Related Data Performance
The Related Data API performance has been improved. For example, when you try to retrieve the related data items for specific collection of items, you set this collection to the context and after the first related item is requested, Sitefinity loads all the related data items per property for this collection. After this, each time a request for the related data items from the API is made, Sitefinity returns the results for them from the context (and not from the provider).
void SetRelatedDataSourceContext(this IEnumerable<IDataItem> items)
Sets a collection of items to the context. This collection is then used to retrieve the related data items per field as soon as the first item from this collection requests its related data items.