Add News as related data field
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:
C#
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?
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.