© 2019 by Sayantan Basu

  • Sayantan Basu

Chit-Chat with Event System in Web 8

Event System in web 8 is more or less same as the previous version (Tridion 2013 and 2013 SP1). But the only difference between them is retrieving target types at the time of Auto Publishing. I will walk through the new Event System for web 8 which will help you to understand how to publish pages automatically on page creation.

Changes in Web 8 over Tridion 2013/2013 Sp1:

In the previous version we have to define the Publishing Target or Target Type before calling the publish method. Because it expect the below items as a parameter –


Signature of Publish Method: PublishEngine.Publish(IEnumerable<IdentifiableObject> items,PublishInstruction publishInstruction, IEnumerable<TargetType> targetTypes, PublishPriority publishPriority); or PublishEngine.Publish(IEnumerable<IdentifiableObject> items,PublishInstruction publishInstruction,IEnumerable<PublishingTarget> publishingTargets, PublishPriority publishPriority);

Now in web 8 there is a new concept introduced as “Purpose” to manage all the publishing target settings directly into the Topology Manager. So In web 8, instead of defining the Target Type / Publishing Target you can also define the Purpose which I think is more user-friendly and meaningful for a developer.


Signature of Publish Method:

PublishEngine.Publish(IEnumerable<IdentifiableObject> items,PublishInstruction publishInstruction,IEnumerable<string> purpose, PublishPriority publishPriority);

Remember this is an additional option provided in SD Web 8. The older options are also available.

Let’s create a sample event system project in web 8.

Tool Used:  Visual Studio 2013

1st:

Create a new Class Library project in Visual Studio.

Remember that you have to build your project with .Net 4.5.2 Framework.

2nd:

Add the references into your project as per your requirement. Here I have included below references –

using Tridion.Common; using Tridion.ContentManager; using Tridion.ContentManager.Publishing; (It is required when you want to publish any item from code) using Tridion.ContentManager.Extensibility; using Tridion.ContentManager.Extensibility.Events; using Tridion.Logging; (It is required when you want to generate the log into the Event Viewer)

3rd:

Next you have to define the an unique attribute in your Event System class [TcmExtension("Your Unique Class Name")].

4th:

Then create your class by extending the Tridion.ContentManager.Extensibility.TcmExtension . namespace MyCustomEvent { [TcmExtension("PublishPageEvent")] public class PublishPageEvent : TcmExtension { } }

5th:

Now create the constructor and implement the signature of the Subscribe( Subscribe synchronously to an event) or SubscribeAsync (Subscribe asynchronously to an event) method in it. You can choose one of them as per the requirement. There is no specific reason for it but I personally prefer to define that signature within another method and call that method from my constructor.As you can see here I have created an additional MySubscribe() method and defined the signature of Subcribe under that. Now I am calling that MySubscribe Method from my Constructor.

public PublishPageEvent() { MySubscribe (); } public void MySubscribe () { //here I have define the signature of the Subcribe method //Subscribe<TSubject, TEvent>(TcmEventHandler<TSubject, TEvent> eventHandler, EventPhases phases, EventSubscriptionOrder order) //This signature is same for both Subscribe and SubscribeAsync method.

EventSystem.Subscribe<IdentifiableObject, SaveEventArgs>(OnPageSave, EventPhases.TransactionCommitted); }

6th:

Your code structure is ready, now you have to implement the logic in that method private void OnPageSave(IdentifiableObject subject, EventArgs e, EventPhases phase) { string pageId = subject.Id.ItemId.ToString(); //This will help you to generate the log into the Event Viewer Logger.Write(String.Format("pageId: {0}", pageId), "PublishPageEvent", LoggingCategory.General, System.Diagnostics.TraceEventType.Information);

try { Page page = subject as Page; Session _session = page.Session; Logger.Write(String.Format("Page _ID: {0}", page.Id.ToString()), "PublishPageEvent", LoggingCategory.General, System.Diagnostics.TraceEventType.Information); IEnumerable<IdentifiableObject> items = new List<IdentifiableObject> { page };

/*in the previous version we have to define the publishing target before calling the publish method. Now in web 8 there is new concept introduced “Purpose” to manage all the publishing target settings directly into the Topology Manager. So In web 8 instead of defining the target you can also define the Purpose which I think more user-friendly and meaningful for a developer.*/ string[] purpose = new[] { "Staging" };

//Now you have to define the Publish Instruction detail PublishInstruction instruction = new PublishInstruction(_session) { DeployAt = DateTime.Now, MaximumNumberOfRenderFailures = 5, RenderInstruction = new RenderInstruction(_session), ResolveInstruction = new ResolveInstruction(_session), StartAt = DateTime.MinValue };

PublishEngine.Publish(items, instruction, purpose, PublishPriority.High);

Logger.Write(String.Format("Success Message: {0}", "Item send for Publishing"), "PublishPageEvent", LoggingCategory.General, System.Diagnostics.TraceEventType.Information); } catch (Exception ex) { Logger.Write(String.Format("Exception Message: {0}", ex.Message), "PublishPageEvent", LoggingCategory.General, System.Diagnostics.TraceEventType.Information); } }

7th:

So now you are ready for the event system code deployment into the CMS server. Deployment Procedure:

  1. Copy the compiled DLL to the CMS Server under “<TRIDION_HOME>\bin\”

  2. Open the Tridion.ContentManager.config file from “<TRIDION_HOME>\config\” folder

Add the dll to the extensions:<add assemblyFileName=”<TRIDION_HOME>\bin\Your Event System Code.dll”/>

  1. Restart COM+ service

  2. Restart the Tridion Services

Summary:

Hope this blog helps you to understand how to setup Event System Code in Web 8. I have also uploaded the code in GitHub.You can download it from here.

#AutoPublishing #EventSystem #SDLWeb8