Created
May 29, 2019 09:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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