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 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.

Sitecore Save and Publish Button

I’ve noticed a common question with teaching new content editors how to create and publish content in Sitecore. “I’ve created my content and saved it, how do i get the content live on the site”

As we know saving content only writes the item to the Master database so the content appears in a draft status. The content editor then needs to publish the content to the web database for it to appear on the web site.

Some Sitecore instances don’t take advantage of workflow because the client doesn’t want or need it. In these cases the current method of saving the content and then navigating to the Publishing tab in the ribbon and clicking the publish button seems a little long winded.

Current Sitecore Content Editor Ribbon
Current Ribbon

This made me think of my umbraco days, umbraco has a save button to place content into preview mode. It also has a save and publish button, I wondered if I could replicate this idea within Sitecore.

Within Sitecore it’s easy to extend the ribbon in the content editor. The ribbon is controlled by items created within the content editor section of the core database.

I wanted to replicate exactly what the save and publish buttons did. The button commands are controlled by the Commands.config in the app_config folder. This shows me which classes in the Sitecore Kernel class were used for the commands using dependency injection.

commands.config
commands.config
Using the Commands.Config its easy to find out which classes are responsible for functions within the content editor. Using this i was able to find the classes responsible for the save action and for the publish action. Using JustDecompile (an excellent DLL decompiler) with the Sitecore kernel and the namespace information taken from the commands.config I was able to extract the code for the save command and the publish command.
It was an easy task for then create a new class implementing the Command class and replicating both the save and the publish command code together in the same class and register the command to a button in the content editor and extending the Commands.Config to point at my new class..
mike robbins commands config
Extending Commands Config

View the Package on Sitecore Marketplace View

Source Code GitHub