Adding an Item Informaton button to the Sitecore Page Editor


I had a request to see Item information from the Page Editor without going into the split screen edit item window so I created an Item Information button and corresponding page.

It currently display the following

  • Item ID
  • Path
  • Latest Version
  • Current Workflow
  • Workflow State
  • References
  • Referrers
  • Published State in all Target Databases

One thing to note is it’s for Content Authors and not Dev’s so it’s does’t report on References or Referrers unless there path is one of a Content Item or Media Item.

Here’s the Code for the Command

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Shell.Applications.WebEdit.Commands;
using Sitecore.Web.UI.Sheer;
using Sitecore.Data;
using Sitecore.Data.Items;

namespace Sitecore.Prototypes.PageEditor.WebEdit.Commands
{
public class Info : WebEditCommand
{
public override void Execute(Shell.Framework.Commands.CommandContext context)
{
Item item = context.Items[0];

SheerResponse.ShowModalDialog("/MyApplications/PageEditorInfo/Info.aspx?ItemId=" + item.ID.ToString(), "640px", "480px", string.Empty, false);
return;
}
}
}

Here’s the Code for the Page located in /MyApplications/PageEditorInfo/Info.aspx (yeah lots of literals and inlines CSS)


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Info.aspx.cs" Inherits="Sitecore.Starterkit.MyApplications.PageEditorInfo.Info" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><asp:Literal runat="server" ID="litItemTitle" /></title>
<link rel="stylesheet" href="/sitecore/shell/themes/standard/default/Default.css"/>
<link rel="stylesheet" href="/sitecore/shell/themes/standard/default/Dialogs.css"/>
</head>
<body style="background: none repeat scroll 0% 0% rgb(233, 233, 233);" scroll="no">

<form id="form1" runat="server">
<div style="background-color: White; border-bottom: 1px solid black;padding: 6px;">
<div style="color: black; padding: 0px 0px 4px; font: bold 9pt tahoma;">Item Information for : <asp:Literal runat="server" ID="itemDisplayName" /></div>
<div style="color: rgb(51, 51, 51);">Displays Item Information from the Page Editor</div>
</div>
<div style="padding:6px;">
<table>
<tbody>
<tr>
<td align="right">Id : </td><td><asp:Literal runat="server" ID="itemId" /></td>
</tr>
<tr>
<td align="right">Path : </td><td><asp:Literal runat="server" ID="itemPath" /></td>
</tr>
<tr>
<td align="right">Latest Version : </td><td><asp:Literal runat="server" ID="itemVersion" /></td>
</tr>
<tr>
<td align="right">Workflow : </td><td><asp:Literal runat="server" ID="itemWorkflow" /></td>
</tr>
<tr>
<td align="right">Worfklow State : </td><td><asp:Literal runat="server" ID="itemWorkflowState" /></td>
</tr>
<tr>
<td align="right" valign="top">Referrers : </td><td><asp:Literal runat="server" ID="itemReferrers" /></td>
</tr>
<tr>
<td align="right" valign="top">References : </td><td><asp:Literal runat="server" ID="itemReferences" /></td>
</tr>
<tr>
<td align="right" valign="top">Publishing Information : </td><td><asp:Literal runat="server" ID="itemPublishingInformation" /></td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>

and the code behind


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Workflows;
using Sitecore.Data;

namespace Sitecore.Starterkit.MyApplications.PageEditorInfo
{
public partial class Info : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Assert.IsNotNullOrEmpty(Request.QueryString["ItemId"],"ItemId must not be null");

Item item = Sitecore.Context.Database.SelectSingleItem(Request.QueryString["ItemId"]);

Assert.IsNotNull(item,"Item must not be null");
Assert.IsTrue(item.Access.CanRead(), "Read Access Denied");

itemId.Text = item.ID.ToString();
itemPath.Text = item.Paths.FullPath;
litItemTitle.Text = item.DisplayName;
itemDisplayName.Text = item.DisplayName;
itemVersion.Text = item.Versions.GetLatestVersion().Version.Number.ToString();

IWorkflow workflow = Sitecore.Context.Database.WorkflowProvider.GetWorkflow(item);
if (workflow != null)
{
WorkflowState state = workflow.GetState(item);
itemWorkflow.Text = workflow.Appearance.DisplayName;
itemWorkflowState.Text = state.DisplayName;

}
var refrences = Globals.LinkDatabase.GetReferences(item);
foreach (var link in refrences)
{
var linkItem = link.GetTargetItem();
if (linkItem != null && (linkItem.Paths.IsContentItem || linkItem.Paths.IsMediaItem))
itemReferences.Text += linkItem.Paths.FullPath + "<br/>";
}
var referrers = Globals.LinkDatabase.GetReferrers(item);
foreach (var link in referrers)
{
var linkItem = link.GetSourceItem();
if (linkItem != null && (linkItem.Paths.IsContentItem || linkItem.Paths.IsMediaItem))
itemReferrers.Text += linkItem.Paths.FullPath + "<br/>";
}

ShowPublishingInfo(item);
}
void ShowPublishingInfo(Item currentItem)
{
var publishingTargets = Sitecore.Context.Database.SelectSingleItem("/sitecore/system/Publishing targets").Children;

foreach (Item publishingTarget in publishingTargets)
{
string dbName = publishingTarget.Fields["Target database"].GetValue(true);
if (!string.IsNullOrEmpty(dbName))
{
var database = Sitecore.Configuration.Factory.GetDatabase(dbName);

Item item = database.SelectSingleItem(currentItem.ID.ToString());

string htmlOutput = string.Empty;
if (item != null)
{

foreach (var language in item.Languages)
{
Item languageVersion = item.Versions.GetLatestVersion(language);
if (languageVersion != null && languageVersion.Versions.Count > 0)
{
htmlOutput += string.Format("<div>{0} - {1}</div>", languageVersion.Version.Number.ToString(), languageVersion.Language.GetDisplayName());
}
}

}
if (htmlOutput == string.Empty)
{
htmlOutput = "This item is not currently published to " + publishingTarget.DisplayName + ".";
}

htmlOutput = string.Format("<div><div style='font-weight:bold'>Published to {1}</div>{0}</div>", htmlOutput,publishingTarget.DisplayName);

itemPublishingInformation.Text += (htmlOutput);
}
}
}
}
}

You have to add the command to the commands.config file under the app_config folder

<command name=”webedit:info” type=”Sitecore.Prototypes.PageEditor.WebEdit.Commands.Info,Sitecore.Prototypes.PageEditor”/>

Add add the Item to the Core Database

Path : /sitecore/content/Applications/WebEdit/Common Field Buttons/Info

Template : /sitecore/templates/System/WebEdit/WebEdit Button

Header : Item Info

Click : chrome:common:edititem({command:”webedit:info”})

Type : common

Icon : Applications/16×16/information.png

I’ll try to get in a nice downloadable zip format.

Update : I fixed some bugs in the code and created a Sitecore Package available here

Advertisements

Displaying Item Name in Page Editor via Chrome Data


When working with the Sitecore Page Editor there isn’t anything telling you what Item the Field you are editing belongs to. A content author pointed this out to me so I thought it would be nice to come up with a solution.

I found it was quite easy to add this functionality along with the number of items referencing the Item by manipulating display name that is set in the GetChromeData Pipeline.

Here’s the code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.GetChromeData;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;

namespace Sitecore.Prototypes.PageEditor.Pipelines.ChromeData
{
class FieldName : GetChromeDataProcessor
{
// Fields
public const string ChromeType = "field";
public const string FieldKey = "field";

public override void Process(GetChromeDataArgs args)
{
Assert.ArgumentNotNull(args, "args");
Assert.IsNotNull(args.ChromeData, "Chrome Data");
if ("field".Equals(args.ChromeType, StringComparison.OrdinalIgnoreCase))
{
Field argument = args.CustomData["field"] as Field;
string format = "{0}";
Item item = args.Item;
if (item != null)
{
format = item.DisplayName + " ({0})";
var referenceCount = Globals.LinkDatabase.GetReferrers(item).Count();
if (referenceCount > 0)
{
format += " " + referenceCount.ToString() + " References";
}
}

args.ChromeData.DisplayName = string.Format(format,argument.DisplayName);
if (!string.IsNullOrEmpty(argument.ToolTip))
{
args.ChromeData.ExpandedDisplayName = string.Format(format,argument.ToolTip);
}
}
}

}
}

To configure it add the processor to the pipeline

<getChromeData>
<processor type=”Sitecore.Pipelines.GetChromeData.Setup, Sitecore.Kernel”/>
<processor type=”Sitecore.Pipelines.GetChromeData.GetFieldChromeData, Sitecore.Kernel”/>
<processor type=”Sitecore.Prototypes.PageEditor.Pipelines.ChromeData.FieldName,Sitecore.Prototypes.PageEditor”/>
<processor type=”Sitecore.Pipelines.GetChromeData.GetWordFieldChromeData, Sitecore.Kernel”/>
<processor type=”Sitecore.Pipelines.GetChromeData.GetRenderingChromeData, Sitecore.Kernel”/>
<processor type=”Sitecore.Pipelines.GetChromeData.GetEditFrameChromeData, Sitecore.Kernel”/>
<processor type=”Sitecore.Pipelines.GetChromeData.GetPlaceholderChromeData, Sitecore.Kernel”/>
</getChromeData>

Also consider adding this customization by John West and Alex Shyba that fixes an issue with Sitecore not including Datasource References in the Link Database.

http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/07/Add-Presentation-Component-Data-Sources-to-the-Links-Database-in-the-Sitecore-ASPNET-CMS.aspx