Home > C#, Sitecore 6 > Running Sitecore Field Editor from a command

Running Sitecore Field Editor from a command

When working with Sitecores page editor sometimes you need to edit Hidden fields for examples page metadata ie. “Page Title, Metakeywords, Metadescription”.  You could probably use the editframe control and if in pageedit render out the metadatacontroll through a placeholder. But a more elegant way could be by adding a button in editor Ribbon see screenshot below, and executing  the page editor with a limited set of fields, and is this post we would create a general command tha does exactly that.

To make the command as general as possible in would be tied to a settings item of the type
“/sitecore/templates/System/WebEdit/Field Editor Button”

Which contains fields for displaying

  • Header
  • Editor Icon
  • List with fields that should be shown in the editor separated with “|”

And even better you can use this where you are using the sc:editframe.

Now you can add a button of you liking, you could make a chunk here:

/sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Page Editor/Metadata

Under the chunk add a button
/sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Page Editor/Metadata/Edit

The click event
“command:executefieldeditor(path=/sitecore/content/Applications/WebEdit/Edit Frame Buttons/Metadata/EditMetadata)”

executes the command “command:executefieldeditor” specified in the commands.config file.

Mine looks like this

  <command name="command:executefieldeditor" type="PT.Framework.ExecuteFieldEditorCommand.ExecuteFiledEditor,PT.Framework.ExecuteFieldEditorCommand"/>

Now to the code, by deriving from the “FieldEditorCommand” i get the execution of the page editor for free all i have to do is specifying the correct parameters in the virtual method “GetOptions”  , note that the “PageEditFieldEditorOptions” is located in the Sitecore.Client.dll.

<
public class ExecuteFiledEditor : FieldEditorCommand
{
  protected override PageEditFieldEditorOptions GetOptions(ClientPipelineArgs args, NameValueCollection form)
  {
    EnsureContext(args);
    return new PageEditFieldEditorOptions(form, BuildListWithFieldsToShow()) { Title = SettingsItem[HEADER], Icon = SettingsItem[ICON] };
  }

  private void EnsureContext(ClientPipelineArgs args)
  {
    CurrentItem = Database.GetItem(ItemUri.Parse(args.Parameters[URIPARAMETER]));
    Assert.IsNotNull(CurrentItem, CURRENTITEMISNULL);
    SettingsItem = Client.CoreDatabase.GetItem(args.Parameters[PATHPARAMETER]);
    Assert.IsNotNull(SettingsItem, SETTINGSITEMISNULL);
  }
 private List<FieldDescriptor> BuildListWithFieldsToShow()
  {
    List<FieldDescriptor> fieldList = new List<FieldDescriptor>();
    ListString fieldString = new ListString(SettingsItem[FIELDNAME]);
    foreach (string field in new ListString(fieldString))
     if (CurrentItem.Fields[field] != null)
      fieldList.Add(new FieldDescriptor(CurrentItem, field));

   return fieldList;
  }

 private const string FIELDNAME = "Fields";
 private const string HEADER = "Header";
 private const string ICON = "Icon";

 private const string URIPARAMETER = "uri";
 private const string PATHPARAMETER = "path";
 private const string CURRENTITEMISNULL = "Current item is null";
 private const string SETTINGSITEMISNULL = "Settings item is null";

private Item CurrentItem { get; set; }
 private Item SettingsItem { get; set; }

}

And the final result:

Categories: C#, Sitecore 6 Tags: ,
  1. 02/07/2012 at 11:24

    Hi Thomas,

    I tried your solution, cause I need the same kind of functionality on a project I’m currently doing. The click on the Meta data button works great. However, when you now click for example on the Insert button in the Ribbon, you will get an error! And it won’t open up the http:///sitecore/shell/default.aspx?xmlcontrol=Applications.WebEdit.Dialogs.AddMaster Add New dialog.

    In my case I’ll get an error. Could you confirm the error, that it also happens in your proposed solution?

    Thanks in advance, I’ll be investigating the failure also.

    gr,

    Robbert Hock

    • 02/07/2012 at 14:51

      Just to be clear, you pressing the insert ned item button ? And could you paste the error from log ? I haven’t seen any problems with the standard the standard functionality in Sitecore When i using this solution. But post the error and I Will have a look.

      Regards
      Thomas Stern

  2. 02/07/2012 at 11:29

    Hi Thomas,

    Never mind the problem, probably something went wrong with my solution. It works like a charm 😉

  3. 02/07/2012 at 14:53

    Perfect , glad it Worked out.

  4. 14/12/2012 at 10:33

    Been looking for a solution like this for days. Great work!

  5. 14/12/2012 at 10:46

    Any chance I can hook the “ok” and “cancel” events on that form?

    • 14/12/2012 at 11:05

      Hmm I think the entire popup is made with XAML so maybee you could overwrite that one,. I dont know how easy that would be.
      I haven’t verfied this but i think the XAML control for the ok-cancel buttons is this one
      sitecore\shell\Controls\Dialogs\OKCancelButtons.xml

      This control calls to action dialog:ok and dialog:cancel.

      Hope this will get you on your way,

      Regards.

      • 14/12/2012 at 13:49

        Thank you for your reply!

        I found a clean solution:

        protected new void StartFieldEditor(ClientPipelineArgs args)
        {..)

        In that method you can check if it’s a postback or not. If it is, you can check if the result is true or false. True means the user clicked ‘ok’, False means he clicked ‘cancel’.

      • 14/12/2012 at 14:08

        Ok Perfect.

  1. 21/05/2012 at 12:57
  2. 24/07/2012 at 19:23
  3. 10/04/2013 at 14:37
  4. 11/04/2013 at 21:40
  5. 13/04/2013 at 08:00
  6. 02/03/2015 at 09:05

Leave a comment