Skip to content

Instantly share code, notes, and snippets.

@rsoeteman
Created May 29, 2019 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsoeteman/31020dbeac179042a4a2cf7d1274cbc2 to your computer and use it in GitHub Desktop.
Save rsoeteman/31020dbeac179042a4a2cf7d1274cbc2 to your computer and use it in GitHub Desktop.
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