In my previous blog at the beginning, I mentioned about wanted to understand how Sitecore’s fields with Context Menu works. I have to admit that I had fun investigating about it, thus decided to think about what other things that I can do with Context Menus on Sitecore CMS, and implement it. So in today’s blog I will share about how we can go directly to the selected Item on a Droplink field.

Before Getting Started

It is important to note here that the idea behind this blog has already been discussed before except that I’m taking a slight different approach to it. The difference is that instead of going directly to the selected Item on the Droplink field, I’m opening it on a new Content Editor window.

Now that this is clear, we can get back to our classic ‘getting started’.

Getting Started

Since Sitecore’s existing field Droplink does not have any Context Menu item under it, you will need to create it in the Core database under Droplink here : `/sitecore/system/Field types/Link Types/Droplink`.

The value in the Message field on our newly created Menu Item is contentlookupex:gotoitem(id=$Target). Note that, it is important to add the `id=$Target`, as you would need it to do a check else the pipeline will run two times (I noticed that while playing around).

Below are the two possible scenarios with my implementation:

  1. When no Item is selected:

2. When Item is selected:

Implementation

The code below is my custom class named LookupEx which inherits Sitecore’s existing class LookupEx with namespace being Sitecore.Shell.Applications.ContentEditor and assembly Sitecore.Kernel.

public class LookupEx : Sitecore.Shell.Applications.ContentEditor.LookupEx
    {
        public override void HandleMessage(Message message)
        {
            Assert.ArgumentNotNull((object)message, nameof(message));
            base.HandleMessage(message);
            if (!(message["id"] == this.ID))
            {
                return;
            }
            
            if (!string.Equals(message.Name, "contentlookupex:gotoitem", StringComparison.CurrentCultureIgnoreCase)) return;
            this.GoTo();
        }
        protected void GoTo()
        {
            if (this.Disabled)
                return;
            Sitecore.Context.ClientPage.Start((object)this, "GotoItem");
        }
        protected void GotoItem(ClientPipelineArgs args)
        {
            Assert.ArgumentNotNull((object)args, nameof(args));
            if (args.IsPostBack)
            {
                return;
            }
            var itemId = this.ServerProperties["Value"].ToString();
            var lang = this.ServerProperties["ItemLanguage"].ToString();
            if (itemId.IsNullOrEmpty())
            {
                SheerResponse.Alert("Select an Item first.");
            }
            else
            {
                var str = new UrlString();
                str.Add("id", itemId);
                str.Add("fo", itemId);
                str.Add("la", lang);
                Windows.RunApplication("Content editor", str.ToString());
            }
        }
    }

The reason as to why I named my custom class the same as Sitecore’s class LookupEx, is because I kept the value inside the Droplink’s Control field as it is (`content:LookupEx`).

Config file

The below config file is the same as the one on my previous blog.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:security="http://www.sitecore.net/xmlconfig/security/">
  <sitecore role:require="Standalone or ContentManagement" security:require="Sitecore"> 
    <controlSources>
    <source mode="on" namespace="{YourNameSpace}" assembly="{YourAssemblyName}" prefix="content"/>
    </controlSources>
  </sitecore>
</configuration>

That’s it for today, stay tuned for the next one which will revolved around Sitecore’s Field Context Menu (Hint: Media Library).