June 23, 2011

Create a custom remote event in sitecore

Recently, I had to clear some cache on the frontend server from my backend.
To do that I have implemented a custom remote event. I did not find some documentation on sdn so here is the procedure (thank you to the Sergey Kravchenko from the sitecore support).

  1. Create your custom remote event class.  Remote event class is a usual class inherited from System.Object and marked with a [DataContract] attribute.
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization;
    
    namespace XXX
    {
        [DataContract]
        public class ClearCacheEvent
        {
            public ClearCacheEvent()
            {
    
            }
        }
    }
    
  2. Create a custom event arguments class, to store the arguments of the event. It also, can be empty. Both Remote event class and event arguments class will be used to add event to the EventQueue.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Sitecore.Events;
    
    namespace XXX
    {
        public class ClearCacheEventArgs : EventArgs, IPassNativeEventArgs
        {
            public ClearCacheEventArgs(ClearCacheEvent @event)
            {
                
            }
        }
    }
    
  3. Create your custom event handler
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Sitecore.Diagnostics;
    using Sitecore.Events;
    
    namespace XXX
    {
        public class ClearCacheEventHandler
        {
            /// <summary>
            /// The methos is the method that you need to implement as you do normally
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public virtual void OnClearCacheRemote(object sender, EventArgs e)
            {
                Log.Info("Tadaa", this);
            }
    
            /// <summary>
            /// This methos is used to raise the local event
            /// </summary>
            /// <param name="event"></param>
            public static void Run(ClearCacheEvent @event)
            {
                Log.Info("ClearCacheEventHandler - Run", typeof(ClearCacheEventHandler));
                ClearCacheEventArgs args = new ClearCacheEventArgs(@event);
                Event.RaiseEvent("clearcache:remote", new object[] { args });
            }
        }
    }
    
  4. Create a custom Hook to bind event from the EventQueue and the "local event" in the CD server web config. This hook will call the method you have added in the event handler ("Run" method of the ClearCacheEventHandler class in our case)
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Sitecore.Events.Hooks;
    
    namespace XXX
    {
        public class ClearCacheHook : IHook
        {
            public void Initialize()
            {
                Sitecore.Eventing.EventManager.Subscribe(new Action(ClearCacheEventHandler.Run));
            }
        }
    }
    
  5. Trigger the event in your code
    ClearCacheEvent inst = new ClearCacheEvent();
    Sitecore.Eventing.EventManager.QueueEvent<ClearCacheEvent>(inst);
    
  6. Configure the web.config (or add it in a separated file in the /include folder it is cleaner)

    The hook:
    <hooks>
        <hook type="XXX.ClearCacheHook, XXX"/>
    </hooks>
    
    The event:
    <events>
        <event name="clearcache:remote">
            <handler type="XXX.ClearCacheEventHandler, XXX" method="OnClearCacheRemote"/>
        </event>
    </events>
    

How does it work?
  1. Your code add an event in the   EventQueue.
  2. The EventQueue is checked by the special task:
    <eventQueue>
        <processingInterval>00:00:02</processingInterval>
    </eventQueue>
    
  3. The Hook call a method who raise the corresponding local event.
  4. Your local is triggered.

June 7, 2011

The page editor is not yet available

I have recently experiment a bug with the webedit mode in sitecore 6.4.1. When I try to add some components on some pages I have this message: "The page editor is not yet available".

In fact, it append if a layout or a sublayout contains an ASP.NET LinkButton with a PostBackUrl attribute

As a workaround, you can wrap your line with PostBackUrl attribute in the “if” clause like this:
if ((!Sitecore.Context.PageMode.IsPageEditor) && (!Sitecore.Context.PageMode.IsPreview))
{
       lbLink.PostBackUrl = Utils.GetUrlFromGeneralLink(tmpItem, "Link");
}

Thank you to the sitecore support. I will maybe see some of you at the London dreamcore :)