Skip to main content

Create an Action Editor

Abstract

Create an Action Editor and set the parameters of a custom save action.

An Action Editor is a dialog box that lets you set some of the parameters of a save action. You can create an Action Editor for your custom save action.

Before creating an Action Editor, refer to the Sitecore Developer Network website for information about XML controls and an XML application.

The Action Editor uses the following working logic:

  • It reads save action parameters in the OnLoad method.

  • It shows save action parameters to the user.

  • After the user has edited the parameters, it saves them to the Save Actions field of the web form using the OnOk method.

Before implementing the Action Editor, you should familiarize yourself with the following code tips:

  • To get current save action parameters:

    string params=HttpContext.Current.Session[Sitecore.Web.WebUtil.GetQueryString("params")] as string;
    NameValueCollection nvParams=ParametersUtil.XmlToNameValueCollection(params);
    
  • To save parameter values, override the OnOk method:

    protected override void OnOK(object sender, EventArgs args)
        {      
          string str3 = ParametersUtil.NameValueCollectionToXml(this.nvParams ?? new NameValueCollection());
          if (str3.Length == 0)
          {
            str3 = "-";
          }
          SheerResponse.SetDialogValue(str3);
          base.OnOK(sender, args);
        }
    
  • To get the current web form item ID:

    Sitecore.Web.WebUtil.GetQueryString("id");
    
  • To get the current language:

    Sitecore.Web.WebUtil.GetQueryString("la", "en");
    
  • To get the current Sitecore database:

    Sitecore.Web.WebUtil.GetQueryString("db");
    

To create an action editor:

  1. Create an XML file that contains the dialog layout:

    <?xml version="1.0" encoding="utf-8" ?>
    <control xmlns:def="Definition" xmlns="http://schemas.sitecore.net/Visual-Studio-Intellisense">
       <SimpleEditor>
          <Stylesheet>
            .scfContent {
            padding-top : 15px;
            }
            .scfFieldScope{ 
            width:100%; margin:7px; 
            }
            .scfFieldLabel { 
            width:40%; 
            } 
            .scfFieldSelect { 
            width:58%;
            position:absolute;
            right:0;
            margin-right:20px; 
            }
          </Stylesheet>
          <FormDialog ID="Dialog" Icon="Software/32x32/step_new.png">
             <CodeBeside Type="Sitecore.Forms.Sample.SimpleEditor,Sitecore.Forms.Sample"/>
            <Border Class="scfContent" Width="100%"  Align="left" Style="overflow:none;">
              <Literal ID="name" Text="Login:" Class="scfFieldLabel"/>
              <Combobox runat="server" ID="login"/>
            </Border>
            <Border Class="scfContent" Width="100%"  Align="left" Style="overflow:none;">
              <Literal ID="pass" Text="Password:" Class="scfFieldLabel"/>
              <Combobox runat="server" ID="password"/>
            </Border>
             <Border Class="scfContent" Width="100%"  Align="left" Style="overflow:none;">
               <Literal ID="domLiteral" Text="Default domain:" Class="scfFieldLabel"/>
               <Combobox runat="server" ID="domain"/>                      
             </Border>
          </FormDialog>
       </SimpleEditor>
    </control>
    
  2. Create an action editor class based on the Sitecore.Web.UI.Pages.DialogForm. Specify the created class as CodeBeside in the XML file. The following code sample is for the action editor class:

    public class SimpleEditor : DialogForm   
    {
        protected Combobox domain;
        protected Combobox login;
        protected Combobox password;
        private NameValueCollection nvParams;
        protected override void OnLoad(EventArgs e)
        {
          base.OnLoad(e);
          if (!Context.ClientPage.IsEvent)
          {
            foreach (Domain d in DomainManager.GetDomains())
            {
              Sitecore.Web.UI.HtmlControls.ListItem item = new Web.UI.HtmlControls.ListItem();          
              item.Value = d.Name;
              item.Header = d.Name;
              domain.Controls.Add(item);
            }
            foreach (FieldItem f in this.CurrentForm.Fields)
            {
              Sitecore.Web.UI.HtmlControls.ListItem item = new Web.UI.HtmlControls.ListItem();
              item.Value = f.ID.ToString();
              item.Header = f.DisplayName;
              login.Controls.Add(item);
              item = new Web.UI.HtmlControls.ListItem();
              item.Value = f.ID.ToString();
              item.Header = f.DisplayName;
              password.Controls.Add(item);
            }
            domain.Value = this.GetValueByKey("DefaultDomain") ?? string.Empty;
            login.Value = this.GetValueByKey("Login") ?? string.Empty;
            password.Value=this.GetValueByKey("Password") ??string.Empty;
          }
        }
        protected void SaveValues()
        {
          this.SetValue("DefaultDomain", domain.Value);
          this.SetValue("Login", login.Value);
          this.SetValue("Password", password.Value);
        }
        protected override void OnOK(object sender, EventArgs args)
        {
          this.SaveValues();
          string str3 = ParametersUtil.NameValueCollectionToXml(this.nvParams ?? new NameValueCollection());
          if (str3.Length == 0)
          {
            str3 = "-";
          }
          SheerResponse.SetDialogValue(str3);
          base.OnOK(sender, args);
        } 
        public void SetValue(string key, string value)
        {
          if (this.nvParams == null)
          {
            this.nvParams = ParametersUtil.XmlToNameValueCollection(this.Params);
          }
          this.nvParams[key] = value;
        }
        public string GetValueByKey(string key)
        {
          if (this.nvParams == null)
          {
            this.nvParams = ParametersUtil.XmlToNameValueCollection(this.Params);
          }
          return (this.nvParams[key] ?? string.Empty);
        }  
        public string Params
        {
          get
          {
            return (HttpContext.Current.Session[Sitecore.Web.WebUtil.GetQueryString("params")] as string);
          }
        }   
        public string CurrentID
        {
          get
          {
            return Sitecore.Web.WebUtil.GetQueryString("id");
          }
        }
        public FormItem CurrentForm
        {
          get
          {
            if (!string.IsNullOrEmpty(this.CurrentID))
            {
              Item innerItem = this.CurrentDatabase.GetItem(this.CurrentID, this.CurrentLanguage);
              if (innerItem != null)
              {
                return new FormItem(innerItem);
              }
            }
            return null;
          }
        }
        public virtual Database CurrentDatabase
        {
          get
          {
            return Factory.GetDatabase(Sitecore.Web.WebUtil.GetQueryString("db"));
          }
        }
        public virtual Language CurrentLanguage
        {
          get
          {
            return Language.Parse(Sitecore.Web.WebUtil.GetQueryString("la", "en"));
          }
        }
      }
    
  3. In the appropriate Save Action item, in the Editor field, specify the editor that you have created.