Saturday 15 June 2019

Check SiteCore Items has ALIASES or Not using GutterRender


Aliases to set up shortened URLs for publicity purposes, but they were finding it difficult to manage the large number of aliases they were creating. Their key issues were remembering which pages had aliases defined, and remembering to delete aliases when they removed the pages they were related to.


Sitecore provides a helpful dialogue for managing aliases, in the Presentation ribbon tab:




Custom gutter renderings are classes based on the "Sitecore.Shell.Applications.ContentEditor.Gutters.GutterRenderer" class. All you have to provide is an implementation for the GetIconDescriptor() method:

namespace SitecoreMaster.Models
{
    public class AliasGutterRenderer: GutterRenderer
    {
        private static Sitecore.Data.ID aliasID = new Sitecore.Data.ID("{54BCFFB7-8F46-4948-AE74-DA5B6B5AFA86}");

        protected override GutterIconDescriptor GetIconDescriptor(Sitecore.Data.Items.Item item)
        {
            GutterIconDescriptor gid = null;
            int aliases = 0;

            try
            {
                aliases = Sitecore.Globals.LinkDatabase.GetReferrers(item)
                    .Select(l => l.GetSourceItem())
                    .Where(s => s.TemplateID == aliasID)
                    .Count();

                if (aliases > 0)
                {
                    gid = new GutterIconDescriptor();
                    gid.Icon = "Network/32x32/spy.png";
                    gid.Tooltip = string.Format("This item has {0} alias{1}", aliases, aliases > 1 ? "es" : "");
                    gid.Click = "item:setaliases(id=" + item.ID + ")";
                }

            }
            catch (Exception exception)
            {
                // this should always succeed - exceptions seem to come
                // if link database is out of date
                // In production code, we'd do something cleverer than
                // this and try to prevent rather than catch the exception

                throw exception;
            }
            
            return gid;
        }
    }
}

If we want to visualise Aliases, we need to write some sort of query to detect if the current item has any Aliases attached to it. There are a couple of ways you can go about this. One way would be to write a query for children of the /Sitecore/system/Aliases item and look for references to the current item’s ID in the Linked Item field. But Sitecore can make our life easier than this – since it maintains something called the Link Database for us. Inside the Sitecore database, it maintains references between items. Whenever you fill in a field which stores a link between two or more items this relationship is stored in the Link Database to make it easy to query these relationships later. You can see this data yourself when you click the Links button in the Navigate tab of the ribbon:


You’ll notice that this example page has lots of links in the database table – it references things like templates, sublayouts etc. But it also shows that it is referred to by an Alias item. And that shows us that we can use the Links Database to do the query we need.


Now, what do we need to do to make this custom gutter rendering available for users? The data for configuring the Content Editor UI lives in the Core database, under /sitecore/content/Applications/Content Editor/Gutters and configuring a new Gutter Renderer is as simple as adding a new item here based on the “Gutter Renderer” template:


The “Header” field here contains the text displayed in the right-click context menu we saw back at the beginning of this post. The “Type” field contains the fully qualified .Net type descriptor for the custom rendering class we defined. As usual for configuring extension types, this is formatted as “, ”.

And once you have configured that for your custom class, you can go back to the Master database and enable the new renderer by right-clicking the gutter:


Our new custom gutter render is now visible, and its selected state will be remembered between user sessions. Once it’s selected, we start to see the alias icon in the gutter for any item which has Aliases defined:


And clicking the icon will launch the Aliases dialogue.


Check Sitecore Items Publish or Not using GutterRender


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....