Monday 29 June 2015

How to hide Quick Info section using code in Sitecore

1 comment
In this blog I am going to explain how to hide the Quick Info section in the content editor using code. We can show/hide Quick Info section manually by using below steps:
  1. Click on top-left burger menu and then click on Application Options from the menu. The Application Options dialog popup will appear.

  2. Check/uncheck Quick Info section checkbox in content editor tab and click on OK.

Whenever a user gets successfully login then Sitecore executes the loggedin pipeline. Below custom processor is added to loggedin pipeline which sets a flag in the user profile to hide Quick Info section:
using Sitecore.Pipelines.LoggedIn;
using Sitecore.Shell;

namespace Sitecore.Ramblings
{
    public class QuickInfo:Sitecore.Pipelines.LoggedIn.LoggedInProcessor
    {
        /// <summary>The process.</summary>
        /// <param name="args">The args.</param>
        public override void Process(LoggedInArgs args)
        {
            const string DefaultQuickInfo = "false";

            Sitecore.Diagnostics.Assert.ArgumentNotNull(args, "args");
            Sitecore.Diagnostics.Assert.IsTrue(
              Sitecore.Security.Accounts.User.Exists(args.Username),
              args.Username);

            var user = Sitecore.Security.Accounts.User.FromName(
              args.Username,
              true);

            Sitecore.Diagnostics.Assert.IsNotNull(user, "user");

            var sitecoreDomain = Sitecore.SecurityModel.DomainManager.GetDomain(
              "sitecore");

            Sitecore.Diagnostics.Assert.IsNotNull(sitecoreDomain, "sitecoreDomain");

            if (user.Domain != sitecoreDomain
              || user.Name.ToLower().EndsWith("\\" + sitecoreDomain.AnonymousUserName))
            {
                Sitecore.Diagnostics.Log.Warn(this + " : unexpected security domain or user : " + user.Name, this);
                return;
            }

            var key = "/" + args.Username + "/UserOptions.ContentEditor.ShowQuickInfo";

            if (!string.IsNullOrEmpty(user.Profile[key]))
            {
                user.Profile[key] = DefaultQuickInfo;
                user.Profile.Save();
            }

            //bool showQuickInfo = UserOptions.ContentEditor.ShowQuickInfo;
        }
    }
}
Then patch below configuration change to add custom processor in loggedin pipeline and save the configuration file in App_Config\Include folder:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <processors>
      <loggedin>
        <processor patch:after="processor[position()=last()]" type="Sitecore.Ramblings.QuickInfo, Sitecore.Ramblings" />
      </loggedin>
    </processors>
  </sitecore>
</configuration>
Now login in Sitecore content editor and Quick Info section will be hidden.

References:
  1. Sitecore Blog by John West
Comments and suggestions are most welcome. Happy coding!

1 comment :

  1. Sergejs Kravcenko3 July 2015 at 22:32

    Very good, but I would improve your code by instead of using


    deviceDefinition.GetRendering(rendering.RenderingID.ToString()).Datasource

    ReplyDelete