danwhite85
10/10/2012 - 1:23 PM

Example of hooking into an Umbraco back-office page using UmbracoClientDependencyLoader.TryCreate

Example of hooking into an Umbraco back-office page using UmbracoClientDependencyLoader.TryCreate

using System;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using ClientDependency.Core;
using umbraco.BusinessLogic;
using umbraco.IO;
using umbraco.presentation.masterpages;
using umbraco.uicontrols;

namespace Our.Umbraco.Whatever
{
	public class Application : ApplicationBase
	{
		public Application()
		{
			umbracoPage.Init += new MasterPageLoadHandler(this.RegisterResource);
		}

		protected void RegisterResource(object sender, EventArgs e)
		{
			// make sure the page is an umbracoPage; otherwise exit
			var page = sender as umbracoPage;
			if (page == null)
				return;

			// specify the pages that the resource is allowed to be used on
			var allowedPages = new[] { "editcontent.aspx", "editmedia.aspx", "editmember.aspx" };
			var path = page.Page.Request.Path.ToLower();

			// check thath the path is allowed
			if (!allowedPages.Any(path.Contains))
				return;

			// make sure there is a body container; otherwise exit
			var container = page.FindControl("body") as ContentPlaceHolder;
			if (container == null)
				return;

			// attempt to find/create the ClientDependency loader for the page
			bool created;
			var loader = UmbracoClientDependencyLoader.TryCreate(page, out created);
			if (loader != null)
			{
				// set the custom path for your plugin ... and load the resources
				loader.AddPath("MyPlugin", IOHelper.ResolveUrl(string.Concat(SystemDirectories.Umbraco, "/plugins/whatever")));
				loader.RegisterDependency(998, "style.css", "MyPlugin", ClientDependencyType.Css);
				loader.RegisterDependency(999, "script.js", "MyPlugin", ClientDependencyType.Javascript);
			}
		}
	}
}