15 December 2010

Sitecore Media URL

For one of my the latest projects the content editors were always trying to get the actual URL to a Sitecore media library item, but there is no easy way to see this in Sitecore. As developers we know it is in the form of /~/media/[ShortID].ashx or /~/media/[Path].ashx, but editors don't want to think about that. They just want it figured out and displayed in front of them.
What I wanted was something similar to the "Quick Info" section that gives develoeprs/admins a quick view into the important properties of a Sitecore item. This is what I came up with.

This minor change to the editor will allow a user to click on the URL of their choice and copy it to the clipboard.
To add this to your site you need a single class and a change to a pipeline.



   1:  /// <summary>
   2:   
   3:  /// The ShowMediaPath class.
   4:   
   5:  /// </summary>
   6:   
   7:  public class ShowMediaInfo
   8:   
   9:  {
  10:   
  11:      private static readonly string SECTION_NAME = "MediaInfo";
  12:   
  13:   
  14:   
  15:      /// <summary>
  16:   
  17:      /// Gets a value indicating whether this section is collapsed.
  18:   
  19:      /// </summary>
  20:   
  21:      /// <value>
  22:   
  23:      ///  <c>true</c> if this section is collapsed; otherwise, <c>false</c>.
  24:   
  25:      /// </value>
  26:   
  27:      private static bool IsSectionCollapsed
  28:   
  29:      {
  30:   
  31:          get
  32:   
  33:          {
  34:   
  35:              UrlString collapsedSections = new UrlString(Registry.GetString("/Current_User/Content Editor/Sections/Collapsed"));
  36:   
  37:              string value = collapsedSections[SECTION_NAME];
  38:   
  39:              return (string.IsNullOrEmpty(value) || (value == "1"));
  40:   
  41:          }
  42:   
  43:      }
  44:   
  45:   
  46:   
  47:      /// <summary>
  48:   
  49:      /// Processes the specified args.
  50:   
  51:      /// </summary>
  52:   
  53:      /// <param name="args">The args.</param>
  54:   
  55:      public void Process(RenderContentEditorArgs args)
  56:   
  57:      {
  58:   
  59:          Item current = args.Item;
  60:   
  61:          if (current != null && current.Template.FullName.StartsWith("System/Media"))
  62:   
  63:          {
  64:   
  65:              MediaItem mediaItem = current;
  66:   
  67:   
  68:   
  69:              if (mediaItem != null)
  70:   
  71:              {
  72:   
  73:                  bool renderMediaInfo = !IsSectionCollapsed || UserOptions.ContentEditor.RenderCollapsedSections;
  74:   
  75:   
  76:   
  77:                  args.EditorFormatter.RenderSectionBegin(args.Parent,
  78:   
  79:                      "MediaInfo",
  80:   
  81:                      SECTION_NAME,
  82:   
  83:                      "Quick Info (Media)",
  84:   
  85:                      "People/32x32/atom.png",
  86:   
  87:                      IsSectionCollapsed,
  88:   
  89:                      UserOptions.ContentEditor.RenderCollapsedSections);
  90:   
  91:   
  92:   
  93:                  if (renderMediaInfo)
  94:   
  95:                  {
  96:   
  97:                      RenderMediaInfo(args, mediaItem);
  98:   
  99:                  }
 100:   
 101:   
 102:   
 103:                  args.EditorFormatter.RenderSectionEnd(args.Parent, renderMediaInfo, true);
 104:   
 105:              }
 106:   
 107:          }
 108:   
 109:      }
 110:   
 111:   
 112:   
 113:      /// <summary>
 114:   
 115:      /// Renders the media info.
 116:   
 117:      /// </summary>
 118:   
 119:      /// <param name="args">The args.</param>
 120:   
 121:      /// <param name="mediaItem">The media item.</param>
 122:   
 123:      private static void RenderMediaInfo(RenderContentEditorArgs args, MediaItem mediaItem)
 124:   
 125:      {
 126:   
 127:          StringBuilder sectionText = new StringBuilder();
 128:   
 129:   
 130:   
 131:          sectionText.Append("<table cellpadding=\"4\" cellspacing=\"0\" border=\"0\">");
 132:   
 133:          sectionText.Append("<col style=\"white-space:nowrap\" align=\"right\" valign=\"top\" />");
 134:   
 135:          sectionText.Append("<col style=\"white-space:nowrap\" valign=\"top\" />");
 136:   
 137:   
 138:   
 139:          // we will give them absolute URL's
 140:   
 141:          MediaUrlOptions o = new MediaUrlOptions { AbsolutePath = true };
 142:   
 143:   
 144:   
 145:          // Get the path of the media item using it's path
 146:   
 147:          o.UseItemPath = true;
 148:   
 149:          sectionText.Append("<tr><td>Url by Path:</td><td>");
 150:   
 151:          sectionText.AppendFormat("<input class=\"scEditorHeaderQuickInfoInput\" readonly=\"readonly\" onclick=\"javascript:this.select();return false\" value=\"{0}\"/>", MediaManager.GetMediaUrl(mediaItem, o));
 152:   
 153:          sectionText.Append("</td></tr>");
 154:   
 155:   
 156:   
 157:          // Get the path of the media item using it's ID
 158:   
 159:          o.UseItemPath = false;
 160:   
 161:          sectionText.Append("<tr><td>Url by ID:</td><td>");
 162:   
 163:          sectionText.AppendFormat("<input class=\"scEditorHeaderQuickInfoInput\" readonly=\"readonly\" onclick=\"javascript:this.select();return false\" value=\"{0}\"/>", MediaManager.GetMediaUrl(mediaItem, o));
 164:   
 165:          sectionText.Append("</td></tr>");
 166:   
 167:   
 168:   
 169:          // is it File or DB media?
 170:   
 171:          sectionText.Append("<tr><td>Media Location:</td><td>");
 172:   
 173:          sectionText.Append(mediaItem.FileBased ? "File System" : "Database");
 174:   
 175:          sectionText.Append("</td></tr>");
 176:   
 177:   
 178:   
 179:          sectionText.Append("</table>");
 180:   
 181:   
 182:   
 183:          args.EditorFormatter.AddLiteralControl(args.Parent, sectionText.ToString());
 184:   
 185:      }
 186:   
 187:  }
You will need also to modify a pipeline. I would suggest creating a ShowMediaInfo.config in the /App_Config/Include directory with the following:


   1:  <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
   2:   
   3:    <sitecore>
   4:   
   5:      <pipelines>
   6:   
   7:        <renderContentEditor>
   8:   
   9:          <processor patch:before="processor[@type='Sitecore.Shell.Applications.ContentEditor.Pipelines.RenderContentEditor.RenderSkinedContentEditor, Sitecore.Client']"
  10:   
  11:                     type="NAMESPACE.ShowMediaInfo, ASSEMBLY" />
  12:   
  13:        </renderContentEditor>
  14:   
  15:      </pipelines>
  16:   
  17:    </sitecore>
  18:   
  19:  </configuration>

The idea for this was based on some code that a colleague of mine at Hedgehog Development had written.
Author:- Sean Kearney
Source:- http://seankearney.com/2010/08/default.aspx

No comments:

Post a Comment