Example: Create item relations

NEW TO SITEFINITY?

Sitefinity CMS 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 item relations:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.Utilities.TypeConverters;
using Telerik.Sitefinity.Versioning;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Workflow;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.RelatedData;
namespace SitefinityWebApp
{
public class CreateRelatedDataItemRelation
{
public void CreateItemRelation()
{
NewsManager newsManager = NewsManager.GetManager();
//create some sample items first, to eb able to relate them through code later
var speaker = this.CreateSpeaker();
var newsItem = this.CreateNewsItem(newsManager, Guid.NewGuid(), "MyNewsItem1", "SampleNewsContent");
// create a relation between the news item and the speaker item. The news item should have a relatedData field of type DynamicContent (pointing to the Speakers module) called "speakers"
// the 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();
}
public DynamicContent CreateSpeaker()
{
// Set the provider name for the DynamicModuleManager here. All available providers are listed in
// Administration -> Settings -> Advanced -> DynamicModules -> Providers
var providerName = String.Empty;
// Set a transaction name and get the version manager
var transactionName = "someTransactionName";
var versionManager = VersionManager.GetManager(null, transactionName);
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName, transactionName);
Type speakerType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Speakers.Speaker");
DynamicContent speakerItem = dynamicModuleManager.CreateDataItem(speakerType);
// This is how values for the properties are set
speakerItem.SetValue("Title", "Some Title");
speakerItem.SetString("UrlName", "SomeUrlName");
speakerItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
speakerItem.SetValue("PublicationDate", DateTime.UtcNow);
speakerItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft");
// Create a version and commit the transaction in order changes to be persisted to data store
versionManager.CreateVersion(speakerItem, false);
TransactionManager.CommitTransaction(transactionName);
return speakerItem;
}
public NewsItem CreateNewsItem(NewsManager newsManager, Guid masterNewsId, string newsTitle, string newsContent)
{
//The news item is created as a master. The newsId is assigned to the master.
var newsItem = newsManager.CreateNewsItem(masterNewsId);
//Set the properties of the news item.
newsItem.Title = newsTitle;
newsItem.Content = newsContent;
newsItem.DateCreated = DateTime.UtcNow;
newsItem.PublicationDate = DateTime.UtcNow;
newsItem.LastModified = DateTime.UtcNow;
newsItem.UrlName = Regex.Replace(newsTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
//Recompiles and validates the url of the news item.
newsManager.RecompileAndValidateUrls(newsItem);
//Save the changes.
newsManager.SaveChanges();
//Publish the news item. The published version acquires new ID.
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(NewsItem).FullName);
WorkflowManager.MessageWorkflow(masterNewsId, typeof(NewsItem), null, "Publish", false, bag);
return newsItem;
}
}
}

Want to learn more?

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?