Tuesday, April 14, 2015

Injecting javascript and css to Sitecore Content Editor Page


Sometimes we need to use javascript in our custom fields, to do that we should inject them into Content Editor Page: 


    public class InjectScripts
    {
        public void Process(PipelineArgs args)
        {
            if (Sitecore.Context.ClientPage.IsEvent) return;

            HttpContext current = HttpContext.Current;
            if (current == null) return;

            Page page = current.Handler as Page;
            if (page == null) return;

            string[] strArray = {
               "/sitecore/shell/Controls/Lib/Scriptaculous/Scriptaculous.js",
               "/sitecore/shell/Controls/Lib/Scriptaculous/builder.js",
               "/sitecore/shell/Controls/Lib/Scriptaculous/effects.js",
               "/sitecore/shell/Controls/Lib/Scriptaculous/dragdrop.js",
               "/sitecore/shell/Controls/Lib/Scriptaculous/slider.js",
               "/sitecore/shell/Controls/CustomControls/CustomField/custom.js"
                                };
            foreach (string str in strArray) page.Header.Controls.Add(new LiteralControl(string.Format("", str)));
            
            page.Header.Controls.Add(new LiteralControl(""));
        }
    }

And in config file:



<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <rendercontenteditor>
        <processor patch:before="*[1]" type="CustomField.InjectScripts, CustomField">
      </processor>
     </rendercontenteditor>
    </pipelines>
  </sitecore>
</configuration>


Very Easy!

No comments:

Post a Comment