Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample how we can create relations for CMSImport on publish of an Umbraco content node
using System.Configuration;
using CMSImportLibrary.Helpers;
using Umbraco.Core;
using Umbraco.Core.Services;
namespace UpdateUmbracoRelations
{
public class PublishedEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarted(umbracoApplication, applicationContext);
ContentService.Published += ContentService_Published;
}
private void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
{
foreach (var publishedItems in e.PublishedEntities)
{
var relationKey = $"{ConfigurationManager.AppSettings["CMSImport.UmbracoRelationPrefix"]}{publishedItems.Id}";
var id = DataHelper.GetRelatedIdFromRelation(relationKey,"Content");
if (id == 0)
{
//Relation does not exists, add it from this event
DataHelper.UpdateRelation(publishedItems.Id, relationKey, "Content");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment