In Sitecore, field names alone dont always give context to the data that editors are updating. On template fields you can set the Short Description field – this then shows the text next to the field. Some examples are:
It may be a strict requirement that help text is set on all fields, or help text on all fields matches a given pattern. The command and / or query below shows how to find missing Sitecore help text.
In the sitecore command example the code also modifies the help text to set the first letter as a capital if its lower case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
using System.Collections.Generic; using System.Linq; using System.Text; using ###.Store.Configuration; using Sitecore.Data; using Sitecore.Data.Items; using Sitecore.Data.Managers; using Sitecore.Shell.Framework.Commands; using Sitecore.Web.UI.Sheer; namespace ###.Store.Developer.Commands { /// <summary> /// Find all template fields which either don't have /// help text or dont start with a capital letter /// </summary> public class ValidateHelpTextCommand : Command { public override void Execute(CommandContext context) { //storeTemplateFolder is the root folder for all templates in /sitecore/content/templates Item storeTemplateFolder = context.Items.FirstOrDefault() .Database.GetItem(TemplateIds.SitecoreTemplates.StoreTemplateFolder, LanguageManager.GetLanguage("en")); if (storeTemplateFolder != null) { ScanChildItems(storeTemplateFolder); } } private void ScanChildItems(Item storeTemplateFolder) { Dictionary<string, string> invalidItems = new Dictionary<string, string>(); foreach (Item child in storeTemplateFolder.Axes.GetDescendants()) { if (child.TemplateID == new ID(TemplateIds.SitecoreTemplates.TemplateField)) { string shortDescription = "__Short Description"; string helpText = child.Fields[shortDescription].Value; invalidItems[child.Paths.ContentPath] = helpText; if (!string.IsNullOrEmpty(helpText) && !HelpHasValidValue(helpText)) { using (new EditContext(child)) { //this corrects all the help text, it may be you don't want to apply the same rules child.Fields[shortDescription].SetValue(char.ToUpper(helpText[0]) + helpText.Substring(1), true); } } } } StringBuilder output = new StringBuilder(); foreach (string key in invalidItems.Keys) { if (string.IsNullOrEmpty(invalidItems[key])) { output.AppendLine(string.Concat(key, " is missing help text")); } else if (!HelpHasValidValue(invalidItems[key])) { output.AppendLine(string.Concat(key, " help text starts with lower case.")); } } output.AppendLine("Will now fix all lower case first letters"); SheerResponse.Alert(output.ToString()); } private static bool HelpHasValidValue(string value) { //invert the check so that things like { are considered valid return !char.IsLower(value.Trim().ToCharArray()[0]); } } } |
Note, there are constants available for all fields ids. The text value was used to highlight the __. The code forces the language to ‘en’ since all our templates are created in that language.
You then patch this into the application via:
1 2 3 4 5 6 7 |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> <sitecore> <commands> <command name="item:validatehelptext" type="###.Store.Developer.Commands.ValidateHelpTextCommand, ###.Store.Developer" /> </commands> </sitecore> </configuration> |
Finally, you need to add a button to core and set its click to be ‘item:validatehelptext’
I thought it would be interesting to try the same thing using Sitecore Rocks Query. There are some really handy blog posts on this – have a look at Sitecore Rocks Query Examples for more info.
1 2 |
set language='en'; select @@Name, @#__Short Description#, @@Path from /sitecore/templates/User Defined//*[@@templatekey = 'Template field'] |
I like both these approaches for tracking down missing items – it’s safe to say trawling through fields manually is both time consuming, boring and error prone!