Wednesday 3 July 2019

Navigate to Content Tree, Items Search by ID and Path




Navigate to the Item, Search by Path and Item ID



Lack of a fast way to paste a Sitecore item path into the UI and have the Content Editor change selection to that item. I suggested using the search box or the Navigate button, but apparently, they weren’t right. The person wanted to paste a link and have the content tree change without any other clicking about.



The problem can be solved with a simple Command:


using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Shell.Framework.Commands;
using Sitecore.Web.UI.Sheer;

namespace SitecoreMaster.Models
{
    public class NavigateCommand: Command
    {
        public override void Execute(CommandContext context)
        {
            ClientPipelineArgs args = new ClientPipelineArgs();
            args.CustomData.Add("db", context.Items[0].Database.Name);

            Context.ClientPage.Start(this, "Run", args);
        }

        protected void Run(ClientPipelineArgs args)
        {
            if (!args.IsPostBack)
            {
                SheerResponse.Input("Enter a path or id: ", string.Empty);
                args.WaitForPostBack();
            }
            else
            {
                if (args.HasResult)
                {
                    string dbName = (string)args.CustomData["db"];
                    Database db = Sitecore.Configuration.Factory.GetDatabase(dbName);
                    Item itm = db.GetItem(args.Result);

                    if (itm != null)
                    {
                        Context.ClientPage.SendMessage(this, string.Format("item:load(id={0})", itm.ID.ToString()));
                    }
                    else
                    {
                        SheerResponse.ShowError("Item not found", string.Format("The item '{0}' was not found.", args.Result));
                    }
                }
            }
        }
    }

}


When the command is triggered by clicking a button, the code records the database that the context item was found in. (That’s whatever item was selected in the content tree when the user clicked the button triggering our command) The “context” database for commands is always Core, so we need this information because the user might actually be looking at Master or Web when they click the button. This is then passed into the Run() method.


The first time Run() is triggered no postback has occurred, so we need to ask the user to enter the path or ID of the item to select, and we need to wait for that data. The SheerResponse.Input() method shows a simple dialog with a text entry field. If you were feeling fancy you could create a custom dialog using Sheer here, but this basic dialog will do for our needs.


The second time Run() is triggered the postback from the input dialog should have occurred. If it has, and if the postback has provided us with some data, then we can try and make the Content Tree change focus. To do that we want to load the item to verify it exists. That means extracting the database name from the arguments passed in, fetching a reference to the database and then trying to load the item.




If we get an item back from the database, we can use the SendMessage() method to trigger the “load an item” behaviour. This will cause the Content Editor to load the item specified, refresh the content tree so it has focus, and refresh the editor pane to show the appropriate data. And if we get no item back we can show an error using the ShowError()method



To enable the user to trigger our command we need to wire it up using the same old techniques we’ve used for commands in the past. First, you have to map the class above to a command string by adding an entry to the Commands.config file with a patch



<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <commands>
      <command name="ContentTreenavigate:navigate" type="SitecoreMaster.Models.NavigateCommand,SitecoreMaster" />
    </commands>
  </sitecore>
</configuration>

(Obviously, you need your namespace and dll name in the type attribute above…


Then you need to create a button in the UI to trigger this command. For example, to add it to the “Go to” chunk of the Developer ribbon tab, you can add a new item at /sitecore/content/Applications/Content Editor/Ribbons/Chunks/Goto in the Core database. Using the /sitecore/templates/System/Ribbon/Small Button template add something like:





Choose a suitable icon, fill in the click event with the command name defined above, and give it whatever name you fancy.


So to test it, switch back to Master and click your new button:

If you enter the path "/sitecore/content/Website/Command Template Item/2019/6/New NewsItem"  and click ok, the content tree will change:







If you enter the Item ID "{8E080626-DDC3-4EF4-A1D1-F0BE4A200254}" and click ok, the content tree will change:







Conclusion
Finally, I would like to conclude this post, I hope I have been able to help you and justify the topic.



No comments:

Post a Comment

Sitecore Publishing Service 7.0 Installation Guide

  About the Publishing Service module The Publishing Service module is an optional replacement for the existing Sitecore publishing methods....