October 4, 2012

Open an admin screen in a tab of the content editor

Here is the method to open a tab in the content editor and display an aspx page in it.
Here is an example:

  1. The first you will need is a command. You can add it into your command.config, or in a separate .config in \App_config\Include\. In my example my command is named “commentandrate:opencommenteditoverview” but you can name it as you want of course.
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <commands>
          <command name="commentandrate:opencommenteditoverview" type="MyNamespace.OpenCommentEditOverView, MyDllName"/>      
        </commands>
      </sitecore>
    </configuration>
  2. After that you will only need to write the corresponding class:
    using Sitecore;
    using Sitecore.Diagnostics;
    using Sitecore.Resources;
    using Sitecore.Shell.Framework.Commands;
    using Sitecore.Text;
    using Sitecore.Web;
    using Sitecore.Web.UI.Framework.Scripts;
    using Sitecore.Web.UI.Sheer;
    using Sitecore.Web.UI.XamlSharp.Continuations;
    
    namespace MyNamespace
    {
        public class OpenCommentEditOverView : Command, ISupportsContinuation
        {
            // Methods
            public override void Execute(CommandContext context)
            {
                Assert.ArgumentNotNull(context, "context");
                if (context.Items.Length == 1)
                {
        //Check if the tab is already open and refresh it is needed
                    if (WebUtil.GetFormValue("scEditorTabs").Contains("commentandrate:opencommenteditoverview"))
                    {
                        SheerResponse.Eval("scContent.onEditorTabClick(null, null, 'OpenCommentEditOverView')");
                    }
                    else //Open a new tab
                    {
         //The Path to the aspx file to dsplay in this tab
                        UrlString urlString = new UrlString("/sitecore/shell/Applications/LBi/CommentEditOverView.aspx");
                        context.Items[0].Uri.AddToUrlString(urlString);
                        UIUtil.AddContentDatabaseParameter(urlString);
                        //urlString["fld"] = "__Tracking";
                        SheerResponse.Eval(new ShowEditorTab
                            {
                                Command = "commentandrate:opencommenteditoverview",
                                Header = "Comments", 
                                Icon = Images.GetThemedImageSource("Network/16x16/lock.png"), 
                                Url = urlString.ToString(), 
                                Id = "OpenCommentEditOverView", 
                                Closeable = true
                            }.ToString());
                    }
                }
            }
    
        }
    }
That is it for the command and the code itself but of course you will need a way to call the command.
You can for example add a button in a toolbar.
If you don’t know what is a chunk, a strip, … please refer to this post: http://sitecoreblog.blogspot.be/2010/11/use-contextual-button.html

To create this new button:
  1. Switch to the core database
  2. In /sitecore/content/Applications/Content Editor/Ribbons/Chunks, add a new “Chunk” (template: /sitecore/templates/System/Ribbon/Chunk)
  3. Fill in the fields:
    1. Header: The name who will appear below the block of button
    2. ID: an ID
  4. Below this chunk, add a new item of type /sitecore/templates/System/Ribbon/Large Button
  5. Fill in the fields:
    1. Header: The name who will appear below the block of button
    2. Icon: The icon of this button
    3. Click: you command name (as in the .config)
    4. Tooltip: the text when you let the mouse on this button
  6. To include this chunk in the “Home” toolbar, go in /sitecore/content/Applications/Content Editor/Ribbons/Strips/Home
  7. Add a new subitem (template: /sitecore/templates/System/Reference)
  8. And fill in the Reference field (example of value: sitecore/content/Applications/Content Editor/Ribbons/Chunks/Comment Rate Admin)

No comments:

Post a Comment