3

I am trying to publish a component just after Save event using Event System automatically. I have used the below code.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Tridion.ContentManager;
    using Tridion.ContentManager.CommunicationManagement;
    using Tridion.ContentManager.ContentManagement;
    using Tridion.ContentManager.Extensibility;
    using Tridion.ContentManager.Extensibility.Events;
    using Tridion.ContentManager.Publishing;
    using Tridion.Logging;

    namespace PublishComponentOnSave
    {
        [TcmExtension("PublishComponentOnSaveExtension")]
        public class PublishComponentOnSave : TcmExtension
        {

            public PublishComponentOnSave()
            {
                Subscribe();
            }

            public void Subscribe()
            {
                EventSystem.Subscribe<IdentifiableObject, SaveEventArgs>(PublishComponent, EventPhases.TransactionCommitted); 
            }


            private void PublishComponent(IdentifiableObject subject, SaveEventArgs args, EventPhases phase)
            {
                Component cmp = subject as Component;

                if (cmp.Schema.Title == "Article")
                {

                    IdentifiableObject item = (IdentifiableObject)subject;
                    IEnumerable<IdentifiableObject> items = new List<IdentifiableObject>() { subject };
                    RenderInstruction rndr = new RenderInstruction(subject.Session);
                    rndr.RenderMode = RenderMode.Publish;

                    ResolveInstruction resolveIns = new ResolveInstruction(subject.Session);
                    resolveIns.IncludeComponentLinks = true;

                    PublishInstruction pubInstruction = new PublishInstruction(subject.Session)
                    {
                        DeployAt = DateTime.Now,
                        MaximumNumberOfRenderFailures = 5,
                        RenderInstruction = rndr,
                        ResolveInstruction = resolveIns,
                        StartAt = DateTime.MinValue
                    };

                    TcmUri pubTargeturi = new TcmUri("tcm:0-4-65537");
                    PublicationTarget pubTarget = new PublicationTarget(pubTargeturi, subject.Session);
                    IEnumerable<PublicationTarget> lstPubTarget = new List<PublicationTarget>() { pubTarget };
                    PublishEngine.Publish(items, pubInstruction, lstPubTarget);

                }
            }
        }
    }

But at the time of Publishing the item I am getting an error The item tcm:2033-5959-v0 does not exist." The component Id is tcm:2033-5959.

Looks like the TCM id of the component is changed and the Publish engine can not recognize this. Below is the Event Viewer log

   The item tcm:2033-5959-v0 does not exist.
   Component: Tridion.ContentManager Errorcode: 810 User: NT AUTHORITY\NETWORK     SERVICE  StackTrace Information Details:
   at Tridion.ContentManager.Utilities.AssertItemExists(TcmUri uri, Session session)
  at Tridion.ContentManager.Publishing.PublishEngine.InternalCreatePublishTransactions(IEnumerable`1 items, PublishInstructionBase instruction, IEnumerable`1 targets, Nullable`1 priority)
  at Tridion.ContentManager.Publishing.PublishEngine.CreatePublishTransactions(IEnumerable`1 items, PublishInstructionBase instruction, IEnumerable`1 targets, Nullable`1 priority)
  at Tridion.ContentManager.Publishing.PublishEngine.Publish(IEnumerable`1 items, PublishInstruction publishInstruction, IEnumerable`1 publicationTargets)
  at PublishComponentOnSave.PublishComponentOnSave.PublishComponent(Component subject, SaveEventArgs args, EventPhases phase) in c:\TridionEventHandler\PublishComponentOnSave\PublishComponentOnSave.cs:line 46

Please help.

1 Answer 1

5

For starters your subscribe to IdentifiableObject is a bit risky, since that will capture more than just the save event of a Component, it will trigger on a lot more items in the CM, like Pages, Schemas and Templates (in which case your as cast will cause a problem later in the code). I would suggest you subscribe to the following event:

EventSystem.SubscribeAsync<Component, CheckInEventArgs>(PublishComponent, EventPhases.TransactionCommitted);

I've used an asynchronous event, so it has no influence on the actual save action, and is just executed separately. Also I'm subscribing to the CheckInEventArgs rather than the SaveEventArgs, since you don't need this event to be triggered on every save action, but only on a save and close (i.e. check-in event) when you expect the editor to be done with editing. Since my best guess is that this is the cause of your problem, the save event where your publish method is triggerd on, is the save of a checked out item (hence the -v0 in the URI), which is not an item that can be published like this.

Conlcuding the code, you should be able to publish with the following method:

private void PublishComponent(Component subject, EventArgs args, EventPhases phase)
{
  if (subject.Schema.Title == "Article")
  {
    List<IdentifiableObject> items = new List<IdentifiableObject>() { subject };        
    TcmUri targetTypeUri = new TcmUri("tcm:0-4-65538");     
    List<TargetType> targets = new List<TargetType> { new TargetType(targetTypeUri, subject.Session) };

    PublishInstruction publishInstruction = new PublishInstruction(subject.Session);
    PublishEngine.Publish(items, publishInstruction, targets);      
  }
}

I've simplified your publish code, removing the PublishInstruction as that seemed quite default, and I'm actually using the URI of a Publication Target Type, since that is the item which you would normally select in the CME, when publishing something (note the different item type, I'm just guessting it has the same item ID).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.