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

About Kevin Buckley
.Net web developer with a lot of experience in CMS. Currently working at Sitecore as Solutions Engineer.

One Response to Adding an Item Informaton button to the Sitecore Page Editor

  1. WordPress butchers my whitespace

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: