0

I have configured Tridion event handler for publishing action. In this process I try to modify a component value and then save (check-in) it before it will be published have this content after publish. But this is not being the case when the component is checked-out and the publish date is set to 01-01-0001

    public AddInitialPublishDateToComponent()
    {
        SubScribe();
    }

    private void SubScribe()
    {
        EventSystem.SubscribeAsync<Component, SetPublishStateEventArgs>(OnComponentPublish, EventPhases.Initiated);
        EventSystem.SubscribeAsync<Page, PublishEventArgs>(OnPagePublish, EventPhases.Initiated);
    }

    private void OnPagePublish(Page page, PublishEventArgs e, EventPhases phase)
    {

        if (!e.Targets.Where(x => x.Title.ToLower() == pubTargetStaging).Any() && !e.Targets.Where(x => x.Title.ToLower() == pubTargetLive).Any())
            return;

        if (page.ComponentPresentations != null && page.ComponentPresentations.Where(x => x.Component.Schema.RootElementName == "Article").Any())
        {
            var articleComponent = page.ComponentPresentations.Where(x => x.Component.Schema.RootElementName == "Article").FirstOrDefault().Component;

            Component originalComponent = GetOriginalComponent(articleComponent);

            if (originalComponent == null)
                return;

            UpdateInitialPublishdate(originalComponent);
        }
        else
            return;
    }

    private void OnComponentPublish(Component component, SetPublishStateEventArgs e, EventPhases phase)
    {
        if (e.Target.Title.ToLower() != pubTargetStaging && e.Target.Title.ToLower() != pubTargetLive)
            return;

        var rootElementName = component.Schema.RootElementName;

        if (!allowedSchemas.Contains(rootElementName))
            return;

        Component originalComponent = GetOriginalComponent(component);

        if (originalComponent == null)
            return;

        UpdateInitialPublishdate(originalComponent);
    }

    private void UpdateInitialPublishdate(Component originalComponent)
    {
        var metaFields = new ItemFields(originalComponent.Metadata, originalComponent.Schema);
        var initialPublishDate = (DateField)metaFields["initialPublishDate"];
        if (!string.IsNullOrEmpty(initialPublishDate.ToString()))
        {
            return;
        }

        try
        {
            if (originalComponent.TryCheckOut())
            {
                initialPublishDate.Value = DateTime.Now.AddMinutes(1); // add one minute to prevent conflict with Revisiondate

                originalComponent.Metadata = metaFields.ToXml();
                originalComponent.Save(true);
            }
            else
            {
                originalComponent.CheckIn(true);
                UpdateInitialPublishdate(originalComponent);
            }
        }
        catch (Exception ex)
        {
            Logger.Write($"Inside ex {ex.Message}", EventHandlerName, LogCategory.EventSystem, System.Diagnostics.TraceEventType.Information);
        }
    }

    private Component GetOriginalComponent(Component sharedComponent)
    {
        Component originalComponent = null;
        try
        {
            originalComponent = sharedComponent.OwningRepository.GetObject($"tcm:{sharedComponent.OwningRepository.Id.ContextRepositoryId}-{sharedComponent.Id.ItemId}") as Component;
        }
        catch (Exception ex)
        {
            Logger.Write(ex, EventHandlerName, LogCategory.EventSystem, System.Diagnostics.TraceEventType.Error);
        }

        return originalComponent;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.