This post aims to demonstrate how to add new versions for all languages if they dont exist in the cms.
When you click buttons within the Sitecore client, typically Sitecore commands are used to map these actions to c# code. This link is defined in /App_Config/Commands.config. Some sample entries here are:
1 2 |
<command name="item:addversion" type="Sitecore.Shell.Framework.Commands.AddVersion,Sitecore.Kernel" /> <command name="item:archive" type="Sitecore.Shell.Framework.Commands.Archive,Sitecore.Kernel" /> |
If you want to add your own commands you can either edit this file or setup a patch file in /App_Config/Include. Note the patch file is the preferable option.
The code used for this example is:
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 83 84 85 |
using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; using Sitecore; using Sitecore.Data.Items; using Sitecore.Data.Managers; using Sitecore.Diagnostics; using Sitecore.Globalization; using Sitecore.Shell.Framework.Commands; using Sitecore.Web.UI.Sheer; namespace ###.Domain.Cms.Specialization.Shell.Framework.Commands { /// <summary> /// Create a version of the current item in all languages /// </summary> public class CreateVersionInAllLanguages : Command { public override void Execute(CommandContext context) { Assert.ArgumentNotNull(context, "context"); if (context.Items.Length == 1) { NameValueCollection parameters = new NameValueCollection(); parameters["items"] = base.SerializeItems(context.Items); Context.ClientPage.Start(this, "Run", parameters); } } protected void Run(ClientPipelineArgs args) { Item item = base.DeserializeItems(args.Parameters["items"])[0]; if (item == null) { return; } IEnumerable<Language> languages = LanguageManager.GetLanguages(item.Database).Where(a => a != item.Language); if (SheerResponse.CheckModified()) { //prompt user they want to add versions if (args.IsPostBack) { if (args.Result == "yes") { foreach (Language language in languages) { Item localizedItem = item.Database.GetItem(item.ID, language); //if Versions.Count == 0 then no entries exist in the given language if (localizedItem.Versions.Count == 0) { localizedItem.Editing.BeginEdit(); localizedItem.Versions.AddVersion(); localizedItem.Editing.EndEdit(); } Sitecore.Web.UI.HtmlControls.DataContext contentEditorDataContext = Sitecore.Context.ClientPage.FindControl("ContentEditorDataContext") as Sitecore.Web.UI.HtmlControls.DataContext; contentEditorDataContext.SetFolder(item.Uri); } } } else { StringBuilder builder = new StringBuilder(); builder.Append("Create version for each language?"); SheerResponse.Confirm(builder.ToString()); args.WaitForPostBack(); } } } } } |
This then needs to be added into the commands section of the config with the following patch file:
1 2 3 4 5 6 7 |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <commands> <command name="item:addversiontoalllanguages" type="###.Domain.Cms.Specialization.Shell.Framework.Commands.CreateVersionInAllLanguages,###.Domain.Cms" /> </commands> </sitecore> </configuration> |
If you want this functionality available from a cms button, you need to wire up the button to the command. To do this, switch to the core database.
In this example we will add to the Language chunk of the ribbon (/sitecore/content/Applications/Content Editor/Ribbons/Chunks/Language). You need to create a new button and then setup the data section paying close attention to the Command field. This wants to be the same value as set in the patch file (item:addversiontoalllanguages).