Skip to content
November 12, 2015 / kiranpatils

Generic way to get Item and Database on publish:end and publish:end:remote

Challenge:

Hello Sitecore Basics followers. Sorry for being silent for a while. But was busy with launch and lot of other things. But as was busy with launch learnt lot of things. Which as per our tradition will be sharing with you. So. let’s go!

After deploying stuff for Soft launch we noticed that few of our handlers were not getting Item value on publish:end:remote (If you are new to this, please do let me know. Can share the basic. But :end and :end:remote events gets fired when some event happens :end fires on same server from where action has been triggered e.g. CM and :end:remote fires on remote server e.g. CD ). Then after troubleshooting we found that we used same approach to get item in :end and :end:remote method.

You also have similar challenge? Or this scenario sounds interesting to you and you think you/your team might face this in future. Then let’s see how we overcame this

“The brick walls are there for a reason. The brick walls are not there to keep us out. The brick walls are there to give us a chance to show how badly we want something. Because the brick walls are there to stop the people who don’t want it badly enough. They’re there to stop the other people.” ― Randy Pausch, The Last Lecture

Solution:

This issue was happening with couple of our handlers only. And it was not happening with one of our handler. So, we tried to see what special that handler was doing. And yeah — we found it! We used different approach in not working handler which caused that error. One of our great architect – Daniel Schoenberg has created Helper method which will be helpful for you as well:

namespace HI.SitecoreBasics
{
    public class EventHelper
    {
        public static void GetPublishingInfo(EventArgs args, out Item rootItem, out Database database)
        {
            rootItem = null;
            database = null;

            //Check for a local event first.
            var eventArgs = args as SitecoreEventArgs;
            if (eventArgs != null)
            {
                if (eventArgs.Parameters == null || !eventArgs.Parameters.Any())
                {
                    Log.Fatal("No event args parameter", typeof(EventHelper));
                    return;
                }
                var publisher = eventArgs.Parameters[0] as Publisher;
                if (publisher == null)
                {
                    Log.Fatal("Publisher is null", typeof(EventHelper));
                    return;
                }

                var publishOptions = publisher.Options;
                if (publishOptions == null)
                {
                    Log.Fatal("publishOptions is null", typeof(EventHelper));
                    return;
                }

                rootItem = publishOptions.RootItem;
                database = publishOptions.TargetDatabase;
            }
            if (rootItem != null && database != null) return;

            //There is the posibility of this being a remote event.
            var remoteEventArgs = args as PublishEndRemoteEventArgs;
            if (remoteEventArgs != null)
            {
                if (!string.IsNullOrWhiteSpace(remoteEventArgs.TargetDatabaseName))
                {
                    database = Sitecore.Data.Database.GetDatabase(remoteEventArgs.TargetDatabaseName);
                    if (database != null)
                    {
                        rootItem = database.GetItem(new ID(remoteEventArgs.RootItemId));
                    }
                }
            }
        }
    }
}

Caller:

public void OnItemPublishRemote(object sender, EventArgs args)
{
           if (args == null) return;
            
           Item rootItem;
           Database database;
           EventHelper.GetPublishingInfo(args, out rootItem, out database);
            
           if (rootItem == null) return;

           // Do fun with rootItem and database
}

Simple – Correct? Missed something? Feel free to correct!

If you liked this Credit goes to Daniel Schoenberg – Thank you!

Happy Local and Remote event handling! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: