Showing posts with label Gutter. Show all posts
Showing posts with label Gutter. Show all posts

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


Thursday, 31 January 2019

Check SiteCore Items Publish or Not using GutterRender






What is Gutter in Sitecore?

Gutter also is known as a quick action bar, can be seen when you right click on the left bar of the content tree.

Sitecore provides default gutters to know the
  • Item buckets
  • Cloned Items
  • Personalizations
  • Multivariant Tests
  • My Locked Items
  • Locked items
  • Workflow state
  • Broken links
  • Missing Version
  • Publishing Warnings
  • Validation Rules
  • Presentation overridden

In addition to these default Gutters, we can create custom Sitecore Gutters.


Steps to Create Custom Sitecore Gutter

  1. Create a class which inherits the GutterRenderer.
  2. Override the GutterIconDescriptor method in the GutterRenderer class, to set the appropriate icons to the gutter.
  3. Switch to core database in Sitecore interface.
  4. Create Custom Gutter using the /Sitecore/templates/Sitecore Client/Content editor/Gutter Renderer.
  5. Populate the Header and Type fields of the Gutter, Header field should contain the name you want to give to the Gutter, Type field should contain the fully qualified class name, the dll which contains the class.


  6. Right click on the left side of the Content tree and select your Gutter. see below screen 


  7. The Gutter now indicates the publication status of your items


CUSTOM SITECORE GUTTER TO INDICATE PUBLISH STATUS

Never Published:
Check whether the item is present in the web database if not implies the item has never been published.

Published:
Compare the revision fields of the item in master and web databases, if found same implies that the latest revision has been published to the web.

Published to at least one target:
Check whether the item is present in any one target database if found implies the item has been published in at least on the target database.


Code & Implementation


namespace testforsitecore.Models


{

    public class PublicationStatus : GutterRenderer
    {
        private readonly ID publishingTargetsFolderId = new ID("{D9E44555-02A6-407A-B4FC-96B9026CAADD}");
        private readonly ID targetDatabaseFieldId = new ID("{39ECFD90-55D2-49D8-B513-99D15573DE41}");

        protected override GutterIconDescriptor GetIconDescriptor(Item item)
        {
            bool existsInAll = true;
            bool existsInOne = false;

            // Find the publishing targets item folder
            Item publishingTargetsFolder = Context.ContentDatabase.GetItem(publishingTargetsFolderId);

            if (publishingTargetsFolder == null)
            {
                return null;
            }

            // Retrieve the publishing targets database names
            List<string> publishingTargetsDatabases = publishingTargetsFolder.GetChildren()
              .Select(x => x[targetDatabaseFieldId])
              .ToList();

            // Check for item existance in publishing targets
            publishingTargetsDatabases.ForEach(delegate (string databaseName)
            {
                if (Database.GetDatabase(databaseName).GetItem(item.ID) != null)
                {
                    existsInOne = true;
                }
                else
                {
                    existsInAll = false;
                }
            });

            // Return descriptor with tooltip and icon
            string tooltip = Translate.Text("This item has not yet been published");
            string icon = "People/16x16/flag_red.png";

            if (existsInAll)
            {
                tooltip = Translate.Text("This item has been published to all targets");
                icon = "People/16x16/flag_green.png";
            }
            else if (existsInOne)
            {
                tooltip = Translate.Text("This item has been published to at least one target");
                icon = "People/16x16/flag_yellow.png";
            }

            return new GutterIconDescriptor()
            {
                Icon = icon,
                Tooltip = tooltip,
                //Click = string.Format("item:publish(id={0})", item.ID), // For Click on publish staus icon publish Item dialog box open
                Click = String.Format("item:load(ID={0})", item.ID)  // For Click on publish staus icon which Item is load
            };
        }
    }
  }

Conclusion:

Sitecore Gutters can be very useful since they visually indicate the state of an item which reduces the extra effort for developers. The above implementation of Sitecore Gutter to indicate the Publish Status of an item comes handy when there is no workflow configured for the items.


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