November Happy Hour will be moved to Thursday December 5th.

Per Nergård
Mar 25, 2010
  5540
(0 votes)

Plugin for working with property caption and help texts

Good descriptive caption and help texts for properties are important for making the editors life easier. But in my experience it’s just those bits that get a bit neglected during the development process. Often left to the end and then it’s a tedious (and boring) job going through all the page types or setting up language files.

I guess no developer likes tedious and boring work so I created a plugin for speeding up the process.

It’s an admin area plugin and consists of a (active) language dropdown and four buttons for updating, saving, creating xml, copy values from a language file.

The list of values consists of four columns. The two first (Core) is caption and help text from the property definition(the first property with a specific name is used all others with the same name is ignored). The second  two columns (lang) is caption and help text from the language files if any exist.

The language dropdown specifies from which language to read / save to.

The update button updates the list.

Save updates the property definitions on all page types.

Save to xml creates a language file for the selected language.

Copy updates the core columns with the values from the lang columns if they contain a value. Nothing is saved to the property definitions until clicking the save button.

Here’s the source:

 

Front

   1: <%@ Page Language="c#" Codebehind="PropertyManager.aspx.cs" AutoEventWireup="False" Inherits="EPiServer.Plugin.PropertyManager.PropertyManager" Title="Titel" %>
   2: <%@ Import Namespace="EPiServer.DataAbstraction" %>
   3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:  
   5: <html xmlns="http://www.w3.org/1999/xhtml">
   6:     <head runat="server">
   7:         <title><asp:Literal runat="server" Text="<%$ Resources : EPiServer, attributes.displayname  %>"/> </title>
   8:         
   9:         <link href="<%= ResolveUrl("~/App_Themes/Default/Styles/system.css") %>" type="text/css" rel="stylesheet" />
  10:         <link href="<%= ResolveUrl("~/App_Themes/Default/Styles/ToolButton.css") %>" type="text/css" rel="stylesheet" />
  11:         <script src="<%= EPiServer.UriSupport.ResolveUrlFromUtilBySettings("javascript/episerverscriptmanager.js") %>" type="text/javascript"></script>
   1:  
   2:         <script src="<%= EPiServer.UriSupport.ResolveUrlFromUIBySettings("javascript/system.js") %>" type="text/javascript">
</script>
  12:     </head>
  13:     <body>
  14:     
  15:         <form method="post" id="aspnetForm" runat="server">
  16:             <EPiServer:translate ID="Translate1" runat="server" Text="/propertymanager/button/refresh/text" />
  17:             <span class="EP-systemInfo"><%
   1: # EPiServer.Core.LanguageManager.Instance.Translate("/admin/captions/copypagetypeproperties")
%></span><br /><br />
  18:  
  19:             <asp:PlaceHolder ID="SystemMessageHolder" runat="server" Visible="false">
  20:                 <span class="EP-systemMessage">
  21:                     <table cellspacing="5">
  22:                         <tr>
  23:                             <td><asp:Literal ID="SystemMessage" runat="server" /></td>
  24:                         </tr>
  25:                     </table>
  26:                 </span><br/><br/>
  27:             </asp:PlaceHolder>
  28:             
  29:             <div style="padding-bottom:5px;"><asp:Label AssociatedControlID="selectLanguage" Text="<%$ Resources : EPiServer, propertymanager.label.languages.text  %>" title="<%$ Resources : EPiServer, label.languages.title  %>" runat="server"/> <asp:DropDownList ID="selectLanguage" runat="server" EnableViewState="true" /></div>
  30:             <div>
  31:                 <asp:Button ID="refreshButton" runat="server" Text="<%$ Resources : EPiServer, propertymanager.button.refresh.text  %>" ToolTip="<%$ Resources : EPiServer, propertymanager.button.refresh.title  %>" />
  32:                 <asp:Button ID="saveButton" runat="server" Text="<%$ Resources : EPiServer, propertymanager.button.save.text  %>" ToolTip="<%$ Resources : EPiServer, propertymanager.button.save.title  %>"/>
  33:                 <asp:Button ID="updatePropertyFromXml" Text="<%$ Resources : EPiServer, propertymanager.button.update.text  %>" ToolTip="<%$ Resources : EPiServer, propertymanager.button.update.title  %>" runat="server" />
  34:                 <asp:Button ID="asXmlButton" runat="server" Text="<%$ Resources : EPiServer, propertymanager.button.xml.text  %>" ToolTip="<%$ Resources : EPiServer, propertymanager.button.xml.title  %>" />
  35:             </div>
  36:  
  37:         <asp:ListView ID="lwPropertyDetails" runat="server">
  38:             <LayoutTemplate>
  39:                 <table class="epistandardtable">
  40:                 <tr>
  41:                     <th><asp:Literal ID="Literal1" runat="server" Text="<%$ Resources : EPiServer, propertymanager.list.columns.corename  %>" /></th>
  42:                     <th><asp:Literal runat="server" Text="<%$ Resources : EPiServer, propertymanager.list.columns.corecaption  %>" /></th>
  43:                     <th><asp:Literal runat="server" Text="<%$ Resources : EPiServer, propertymanager.list.columns.corehelp  %>" /></th>
  44:                     <th><asp:Literal runat="server" Text="<%$ Resources : EPiServer, propertymanager.list.columns.langcaption  %>" /></th>
  45:                     <th><asp:Literal runat="server" Text="<%$ Resources : EPiServer, propertymanager.list.columns.langhelp  %>" /></th>
  46:                 </tr>
  47:                 <asp:PlaceHolder id="itemPlaceHolder" runat="server"></asp:PlaceHolder>
  48:                 </table>
  49:             </LayoutTemplate>
  50:             <ItemTemplate>
  51:                 <tr runat="server"><td visible="false" id="pdID" class="EP-tableCaptionCell"><%
   1: #((PageDefinition)Eval("Value")).ID 
%></td><td id="pdName" class="EP-tableCaptionCell"><%
   1: #((PageDefinition)Eval("Value")).Name 
%></td> <td><asp:TextBox ID="pdCaption" CssClass="episize240" runat="server" Text='<%#((PageDefinition)Eval("Value")).EditCaption %>' /> </td><td><asp:TextBox id="pdHelp" CssClass="episize240" runat="server" Text='<%#((PageDefinition)Eval("Value")).HelpText %>' /></td><td><asp:TextBox ID="pdTranslateCaption" runat="server" enabled="false" style="background-color:HighlightText;" CssClass="episize240" Text='<%#GetTranslation(((PageDefinition)Eval("Value")).Name, "caption")%>' /></td> <td><asp:TextBox ID="pdTranslatehelp" runat="server" enabled="false" style="background-color:HighlightText;" CssClass="episize240" Text='<%#GetTranslation(((PageDefinition)Eval("Value")).Name, "help")%>' /></td></tr>
  52:             </ItemTemplate>
  53:         </asp:ListView>
  54:         </form>
  55:     </body>
  56: </html>

CodeBehind

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Web.Security;
   4: using System.Web.UI.WebControls;
   5: using EPiServer.Personalization;
   6: using EPiServer.PlugIn;
   7: using EPiServer.Security;
   8: using EPiServer.Util.PlugIns;
   9: using System.Web.UI;
  10: using EPiServer.DataAbstraction;
  11: using System.Web.UI.HtmlControls;
  12: using System.Xml;
  13: using System.IO;
  14: using System.Configuration;
  15: using EPiServer.Core;
  16: using EPiServer.UI;
  17:  
  18:  
  19: namespace EPiServer.Plugin.PropertyManager
  20: {
  21:     [GuiPlugIn(DisplayName = "Property Definitions Manager", Description = "Plugin for managing of property caption- and helptext", Area = PlugInArea.AdminMenu, Url = "~/Plugin/PropertyManager/PropertyManager.aspx", LanguagePath = "/propertymanager")]
  22:     public partial class PropertyManager : System.Web.UI.Page
  23:     {
  24:         #region Fields
  25:         private string _captionText = "";
  26:         private string _helpText = "";
  27:         private string _propName = "";
  28:         private string _propID = "";
  29:         private string _fromXmlCaptionText = "";
  30:         private string _fromXmlHelpText = "";
  31:  
  32:         const string xmlfilename = "/lang/propertydefinitions_{0}.xml";
  33:         #endregion
  34:  
  35:         #region Methods
  36:         protected override void OnLoad(EventArgs e)
  37:         {
  38:             base.OnLoad(e);
  39:         }
  40:  
  41:         private void UpdatePageTypes()
  42:         {
  43:             foreach (ListViewItem item in lwPropertyDetails.Items)
  44:             {
  45:                 _captionText = ((TextBox)item.FindControl("pdCaption")).Text;
  46:                 _helpText = ((TextBox)item.FindControl("pdHelp")).Text;
  47:                 _propName = ((HtmlTableCell)item.FindControl("pdName")).InnerText;
  48:                 _propID = ((HtmlTableCell)item.FindControl("pdID")).InnerText;
  49:  
  50:                 PageDefinition def = PageDefinition.Load(int.Parse(_propID));
  51:  
  52:                 foreach (PageType pt in PageType.List())
  53:                 {
  54:                     foreach(PageDefinition definition in pt.Definitions)
  55:                     {
  56:                         if (definition.Name.ToLower() == _propName.ToLower())
  57:                         {
  58:                             definition.EditCaption = _captionText;
  59:                             definition.HelpText = _helpText;
  60:                             definition.Save();
  61:                             break;
  62:                         }
  63:                     }    
  64:                 }
  65:             }
  66:         }
  67:      
  68:         private SortedDictionary<string, PageDefinition> GetPropertyDefinitions()
  69:         {
  70:             SortedDictionary<string, PageDefinition> definitions = new SortedDictionary<string, PageDefinition>();
  71:  
  72:             PageTypeCollection pageTypes = new PageTypeCollection();
  73:  
  74:             pageTypes = PageType.List();
  75:  
  76:             foreach (PageType ptype in pageTypes)
  77:             {
  78:                 foreach (PageDefinition definition in ptype.Definitions)
  79:                 {
  80:                     if (!definitions.ContainsKey(definition.Name.ToLower()))
  81:                         definitions.Add(definition.Name.ToLower(), definition);
  82:                 }
  83:             }
  84:  
  85:             return definitions;
  86:         }
  87:  
  88:         private void CreateLangXmlFiles()
  89:         {
  90:             XmlDocument doc = new XmlDocument();
  91:             XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8",null);
  92:             doc.AppendChild(docNode);
  93:  
  94:             XmlNode languagesNode = doc.CreateElement("languages");
  95:             doc.AppendChild(languagesNode);
  96:  
  97:             XmlNode languageNode = doc.CreateElement("language");
  98:             XmlAttribute name = doc.CreateAttribute("name");
  99:             name.Value = selectLanguage.SelectedItem.Text;
 100:  
 101:             XmlAttribute id = doc.CreateAttribute("id");
 102:             id.Value = selectLanguage.SelectedItem.Value;
 103:  
 104:             languageNode.Attributes.Append(name);
 105:             languageNode.Attributes.Append(id);
 106:  
 107:             languagesNode.AppendChild(languageNode);
 108:  
 109:             XmlNode commonNode = doc.CreateElement("common");
 110:             XmlNode pageTypesNode = doc.CreateElement("pagetypes");
 111:  
 112:             languageNode.AppendChild(commonNode);
 113:             commonNode.AppendChild(pageTypesNode);
 114:  
 115:  
 116:             foreach (ListViewItem item in lwPropertyDetails.Items)
 117:             {
 118:                 _captionText = ((TextBox)item.FindControl("pdCaption")).Text;
 119:                 _helpText = ((TextBox)item.FindControl("pdHelp")).Text;
 120:                 _propName = ((HtmlTableCell)item.FindControl("pdName")).InnerText;
 121:  
 122:                 XmlNode propertyNode = doc.CreateElement("property");
 123:                 XmlAttribute propertyName = doc.CreateAttribute("name");
 124:                 propertyName.Value = _propName;
 125:  
 126:                 propertyNode.Attributes.Append(propertyName);
 127:                 XmlNode caption = doc.CreateElement("caption");
 128:                 XmlNode helptext = doc.CreateElement("help");
 129:                 propertyNode.AppendChild(caption);
 130:                 caption.AppendChild(doc.CreateTextNode(_captionText));
 131:                 helptext.AppendChild(doc.CreateTextNode(_helpText));
 132:                 propertyNode.AppendChild(helptext);
 133:  
 134:                 pageTypesNode.AppendChild(propertyNode);
 135:  
 136:             }
 137:  
 138:             doc.Save(Server.MapPath(string.Format(xmlfilename,selectLanguage.SelectedItem.Value)));
 139:  
 140:         }
 141:  
 142:         public string GetTranslation(string propertyname,string node)
 143:         {
 144:             string strang = "/pagetypes/common/property[@name='{0}]'/{1}";
 145:             string kalle = string.Format(strang, propertyname, node);
 146:  
 147:             return LanguageManager.Instance.TranslateRaw("/pagetypes/common/property[@name=\"" + propertyname + "\"]/" + node,selectLanguage.SelectedItem.Value);
 148:         }
 149:  
 150:         protected override void OnInit(EventArgs e)
 151:         {
 152:             base.OnInit(e);
 153:             
 154:             selectLanguage.DataSource = LanguageBranch.ListEnabled();
 155:             selectLanguage.DataTextField = "Name";
 156:             selectLanguage.DataValueField = "LanguageID";
 157:             selectLanguage.DataBind();
 158:  
 159:             refreshButton.Click += new EventHandler(refreshButton_Click);
 160:             saveButton.Click += new EventHandler(saveButton_Click);
 161:             asXmlButton.Click += new EventHandler(asXmlButton_Click);
 162:             updatePropertyFromXml.Click += new EventHandler(updatePropertyFromXml_Click);
 163:         }
 164:  
 165:         private SortedDictionary<string, PageDefinition> UpdatePropertyFromXml()
 166:         {
 167:             SortedDictionary<string, PageDefinition> definitions = new SortedDictionary<string, PageDefinition>();
 168:             PageDefinition tempDef = null;
 169:  
 170:             foreach (ListViewItem item in lwPropertyDetails.Items)
 171:             {
 172:                 _captionText = ((TextBox)item.FindControl("pdCaption")).Text;
 173:                 _helpText = ((TextBox)item.FindControl("pdHelp")).Text;
 174:                 _fromXmlCaptionText = ((TextBox)item.FindControl("pdTranslateCaption")).Text;
 175:                 _fromXmlHelpText = ((TextBox)item.FindControl("pdTranslateHelp")).Text;
 176:                 _propName = ((HtmlTableCell)item.FindControl("pdName")).InnerText;
 177:                 _propID = ((HtmlTableCell)item.FindControl("pdID")).InnerText;
 178:  
 179:                 tempDef = new PageDefinition();
 180:                 tempDef.Name = _propName;
 181:                 tempDef.ID = int.Parse(_propID);
 182:  
 183:                 if (string.IsNullOrEmpty(_fromXmlHelpText))
 184:                     tempDef.HelpText = _captionText;
 185:                 else
 186:                     tempDef.HelpText = _fromXmlHelpText;
 187:  
 188:                 if (string.IsNullOrEmpty(_fromXmlCaptionText))
 189:                     tempDef.EditCaption = _captionText;
 190:                 else
 191:                     tempDef.EditCaption = _fromXmlCaptionText;
 192:  
 193:                 definitions.Add(_propName.ToLower(), tempDef);
 194:             }
 195:  
 196:             return definitions;
 197:         }
 198:  
 199:         #endregion
 200:  
 201:         #region eventhandlers
 202:         void updatePropertyFromXml_Click(object sender, EventArgs e)
 203:         {
 204:          
 205:             lwPropertyDetails.DataSource = UpdatePropertyFromXml();
 206:             lwPropertyDetails.DataBind();
 207:         }
 208:  
 209:         void asXmlButton_Click(object sender, EventArgs e)
 210:         {
 211:             CreateLangXmlFiles();
 212:         }
 213:  
 214:         void saveButton_Click(object sender, EventArgs e)
 215:         {
 216:             UpdatePageTypes();
 217:  
 218:             lwPropertyDetails.DataSource = GetPropertyDefinitions();
 219:             lwPropertyDetails.DataBind();
 220:         }
 221:  
 222:         void refreshButton_Click(object sender, EventArgs e)
 223:         {
 224:             lwPropertyDetails.DataSource = GetPropertyDefinitions();
 225:             lwPropertyDetails.DataBind();
 226:         }
 227:  
 228:         #endregion
 229:     }
 230: }
Mar 25, 2010

Comments

Sep 21, 2010 10:33 AM

Thanks for sharing!

May 15, 2017 02:11 PM

Please login to comment.
Latest blogs
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog