Manage item relations
Sitefinity provides you with an API to work with Related data. RelatedDataExtensions method is implemented to facilitate your work with Related data. The method is located in the Telerik.Sitefinity.RelatedData namespace. The following code sample demonstrates how you can create, edit, and delete item relations. In the example, you have a news item with Related data field “speakers” to dynamic content type Telerik.Sitefinity.DynamicTypes.Model.DevReach.Speaker:
C#
using System;
using System.Linq;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.RelatedData;
using Telerik.Sitefinity.Utilities.TypeConverters;
namespace SitefinityWebApp
{
public class RelatedDataAPI_ManageItemRelations
{
public void ManageItemRelations()
{
// get a master news item
NewsManager newsManager = NewsManager.GetManager();
var newsItem = newsManager.GetItems<NewsItem>().Where(n => n.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master).First();
// get a master dynamic content item of type Speaker
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type speakerType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.DevReach.Speaker");
var speaker = dynamicModuleManager.GetDataItems(speakerType).Where(s => s.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master).First();
// create a relation to speaker. Relation will be available only for the current item state (Master)
newsItem.CreateRelation(speaker, "speakers");
newsManager.SaveChanges();
// on publish, all item's relations are copied from Master to Live state.
newsManager.Lifecycle.Publish(newsItem);
newsManager.SaveChanges();
// get the count of all related speakers of this news item
var relatedSpeakersCount = newsItem.GetRelatedItemsCountByField("speakers");
// get all related speakers of this news item
var relatedSpeakers = newsItem.GetRelatedItems<DynamicContent>("speakers");
// get all related parent items to the speaker (i.e. all news items relating this speaker)
var relatedParents = speaker.GetRelatedParentItems<NewsItem>();
// delete the relation between the news item and the speaker
var relatedSpeaker = relatedSpeakers.First();
newsItem.DeleteRelation(relatedSpeaker, "speakers");
newsManager.SaveChanges();
}
}
}
This section contains
Retrieve related data for collection of items
Optimize performance when retrieving related data items for a specific collection of items.
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your 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 ASP.NET Core and take advantage of its decoupled architecture and modern development model.