Add News as related data field

NEW TO SITEFINITY?

You can add relations to content items through the UI by adding a Related data custom field. For more information, see Add a Related data custom field.

Related data fields can also be added to static modules using the following code:

using System;
using System.Collections.Generic;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.ModuleEditor.Web.Services.Model;
using Telerik.Sitefinity.Modules.News.Web.UI;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Utilities.TypeConverters;
using Telerik.Sitefinity.Web.UI.Fields;
namespace SitefinityWebApp
{
public class AddRelatedDataField
{
public void AddField(string contentTypeFullName, string fieldName)
{
Type contentType = TypeResolutionService.ResolveType(contentTypeFullName);
var customFieldContext = new CustomFieldsContext(contentType);
UserFriendlyDataType userFriendlyDataType = UserFriendlyDataType.RelatedData;
var field = new WcfField()
{
Name = fieldName,
ContentType = contentTypeFullName,
FieldTypeKey = userFriendlyDataType.ToString(),
IsCustom = true,
//Field definition
Definition = new WcfFieldDefinition()
{
Title = fieldName,
FieldName = fieldName,
FieldType = typeof(RelatedDataField).FullName,
AllowMultipleSelection = true,
FrontendWidgetTypeName = typeof(NewsView).FullName, //For Example: news view added for a frontend widget
RelatedDataProvider = string.Empty,
RelatedDataType = typeof(NewsItem).FullName //For Example: news item added as related data type
}
};
Dictionary<string, WcfField> relatedDataFields = new Dictionary<string, WcfField>();
relatedDataFields.Add(fieldName, field);
customFieldContext.AddOrUpdateCustomFields(relatedDataFields, contentType.Name);
customFieldContext.SaveChanges();
//The soft restart of Sitefinity CMS is needed in order to add the recently added custom fields to OA context
// SystemManager.RestartApplication(false); -- This is obsolete
// This is from the documentation: https://docs.sitefinity.com/for-developers-application-restart
// SystemManager.RestartApplication("Restart invoked through the Sitefinity CMS API", SystemRestartFlags.Default, true);
}
}
}

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?