Sitecore Content Editor Tabs In SPEAK

Within the Sitecore Content Editor, the editor section can change context based on the type of Item you are editing. For example when a media folder item is selected, the view allows you to upload media files, a template item allow you to add fields etc. These areas are called editors.

Media Editor
Media Editor

You can define and create your own custom editors within the Content Editor and specify the type of items it is applicable to. Continue reading

Sitecore Replace Tokens Command

Quite often when developing Sitecore sites, as I’m optimising templates I add additional fields and tokens into the standard values. This means for already created items based on the updated template there are unexpanded $token variables in the content item. The same issue is present if you reset a content item to standard values, the tokens are adding into the content item rather than the calculated value. Continue reading

Publish And Send For Translate Workflow Action

Here is a useful Custom Workflow action I’ve written allowing a content editor to publish a content item to live while also sending its alternative language version to a translate workflow state. This allows a content item to be published to live in a single language without being held up waiting on a translation to be completed.

  • 1. Create a workflow command to publish the content to the published state in workflow.
  • 2. Create a new custom workflow action underneath the publish command.
  • 3. Use the code below to automatically create a new language version and set the workflow state to translation state.
  • 4. Replace the parameters in the screenshot of the custom workflow action. Replace the targetstate parameter with the ID of the target language state. Replace language with the name of the target language

Publish And Translate Action

using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Web;
using Sitecore.Workflows.Simple;

namespace MikeRobbins.SitecoreComponents.WorkFlow
{
    public class PublishAction
    {
        public void Process(WorkflowPipelineArgs args)
        {
            Item workFlowItem = args.DataItem;

            var targetLanguage = GetTargetLanguage(args.ProcessorItem.InnerItem);
            var targetVersion = Sitecore.Context.ContentDatabase.GetItem(workFlowItem.ID, targetLanguage);

            if (targetVersion.Versions.Count == 0)
            {
                targetVersion = targetVersion.Versions.AddVersion();
            }

            var targetWorkflowState = GetWorkflowState(args.ProcessorItem.InnerItem);

            SetWorkflowState(targetVersion, targetWorkflowState.ID);
        }

        private static void SetWorkflowState(Item welshVersion, ID targetWorkflowState)
        {
            welshVersion.Editing.BeginEdit();
            welshVersion.Fields[FieldIDs.WorkflowState].Value = targetWorkflowState.ToString();
            welshVersion.Editing.EndEdit();
        }

        private static Item GetWorkflowState(Item actionItem)
        {
            ID translateId = null;

            return ID.TryParse(WebUtil.ParseUrlParameters(actionItem["parameters"])["targetstate"], out translateId) ? Sitecore.Context.ContentDatabase.GetItem(translateId) : null;
        }

        public Language GetTargetLanguage(Item actionItem)
        {
            var language = WebUtil.ParseUrlParameters(actionItem["parameters"])["language"];

            Assert.IsNotNullOrEmpty(language, "Language needs to be specified as a parameter (&language=cy-gb)");

            return LanguageManager.GetLanguage(language);
        }
    }
}

Sitecore Data Importer / Word docx Importer

I recently had a requirement from a client to allow them to import content into Sitecore items from word docx files. After looking on the Sitecore marketplace and git hub I realised nothing existed so decided on writing a module. I did quickly realise why nobody has written one before docx files are basically zip files containing a number of files with the main document content written in XML.

The document

To be useful I wanted the importer to be able to split the docx file into fields within a Sitecore item rather than drop the entire contents into one rich text editor field. This became an issue, how could I tell which section of the document related to which field.

The solution I came up was using the “Title” button in Microsoft Word to mark each section of the document. The title would match up exactly with the field name within Sitecore. Using this structure means that I could programmatically read all content between titles and know which field in Sitecore that content related too.

Sample Document
Sample Document

Import Module

The actual Sitecore data importer module has a simple interface allowing the upload of a file to import (Sitecore data importer supports docx files, csv’s and a zip file containing docx files) . The module also takes the path where you want the items created and the template of the items you want to create.

The main work of the importer parsing the docx file into fields is handled by the code below. Im using OpenXml to help read the docx format, and then iterating through each paragraph checking to see if it’s a title tag, storing the title in a variable and then grabbing the text paragraph(s). Each field name / value is stored in a dictionary which gets written out to the Sitecore API later on.  You can also upload a zip file containing multiple docx files which then gets uncompressed before passing each document off to this function.

Sitecore  Data Importer
Sitecore Data Importer
public Dictionary<string, string> ExtractFields(string path)

{

var fields = new Dictionary<string, string>();

using (var myDocument = WordprocessingDocument.Open(path, true))

{

var body = myDocument.MainDocumentPart.Document.Body;

var dictionaryKey = "";

foreach (var paragraph in body.Where(paragraph => !string.IsNullOrEmpty(paragraph.InnerText)))

{

if (paragraph.InnerXml.Contains("w:val=\"Title\""))

{

dictionaryKey = paragraph.InnerText;

}

else

{

if (fields.ContainsKey(dictionaryKey))

{

fields[dictionaryKey] = fields[dictionaryKey].EndsWith("</p>") ? fields[dictionaryKey] + "<p>" + paragraph.InnerText + "</p>" : "<p>" + fields[dictionaryKey] + "</p><p>" + paragraph.InnerText + "</p>";

}

else

{

fields.Add(dictionaryKey, paragraph.InnerText);

}

}

}

}

return fields;

}

Source Code and Sitecore Marketplace

Any issues or bugs with the module please let me know. The module does need testing with more docx structures, this is the initial release.

https://github.com/komainu85/SitecoreDataImporter

http://marketplace.sitecore.net/en/Modules/Sitecore_Data_Importer.aspx

Registering A New Command

Registering a new Command within the Sitecore Content Editor
1. Create a class that implements the Command Class.
Command Class
Command Class
2. Add the new command to the Commands.Config, You need to specify a command name, the namespace and assembly of the command. (this must match the command for the button in the core database)
mike robbins commands config
Extending Commands Config
3. Add a new button to the Ribbon in the core database. /Sitecore/Content/Applications/Content Editor/Ribbons/(SectionName).
Add new button
Add new button
4. Set the click event of the new button to the command name you specified in the Commands.Config E.g. contenteditor:savePublish
5. Refresh the desktop and switch back to the master database.