Programmatically Add Controls to the HTML Head in Sitecore
I was recently working on a Sitecore module and realized I needed to add some HTML elements to the <head> tag of pages using the module. This would normally be something that can be done directly in every layout, but because this is a Sitecore module, I wanted to do it dynamically via C# through a pipeline so that it would only require a configuration patch to inject my code. The challenge I had was not implementing it via a pipeline processor, but rather programmatically getting access to the <head> tag. Luckily, John West had a great solution, and it was something directly available via the Sitecore API.
First, to accomplish this, register a processor for the renderLayout pipeline:
<renderLayout> <processor type="MyLibrary.Pipelines.RenderLayout.InsertHeadControls, MyLibrary"> ... </renderLayout>
Next, let’s look at the code:
namespace MyLibrary.Pipelines.RenderLayout
{
public class InsertHeadControls
{
public void Process(RenderLayoutArgs args)
{
// no need to run our code in the shell
if(Sitecore.Context.Site.Name == "shell")
return;
Control head = WebUtil.FindControlOfType(Sitecore.Context.Page.Page, typeof(HtmlHead));
if(head != null)
{
head.Controls.Add(/* add whatever control you want */);
}
}
}
}
The key here was to use WebUtil.FindControlOfType() to get the HtmlHead control via C#. From there you can ensure its not null and add controls dynamically to it as needed. Note: for the HtmlHead to not be null, I believe this requires a runat="server" on the <head> tag.
Got anything to say? Go ahead and leave a comment!
Popular Posts
- Using the DataSource Field with Sitecore Sublayouts
- Tame Your Sitecore Treelists
- Write to a Custom Sitecore Log with log4net
- Sitecore Upgrade Strategy
- Rendering Fully Qualified Sitecore URLs
- Sitecore Admin Pages Explained
- Managing CSS in the Sitecore Media Library
- Use Any() Instead of Count() To See if an IEnumerable Has Any Objects
- Sitecore Front-End Development Best Practices
- Scaling Sitecore Presentation Component Data Sources
Recent Comments
- Performance tuning your Sitecore installation | Agile and ALM: Software development today on A Going Live Checklist for Sitecore Websites
- Imran Saleem on Sitecore Avanced Database Crawler Occasionally Provides Null Results
- Ty Cahill on Sitecore Front-End Development Best Practices
- Sitecore Managed Sites as Virtual Folders | Fire Breaks Ice on Sitecore Item and Field Names
- Krimos on Using the DataSource Field with Sitecore Sublayouts
Sitecore Links
- .Sitecore
- Aboo Bolaky
- Alex Shyba
- Anders Dreyer
- aweber1.0
- Brian Pedersen
- Christopher Wojciech
- Coffee => Coder => Code
- Dev Sitecored²
- Everything Web
- Image0.com blog
- John West
- Learn Sitecore
- Let's do Sitecore
- Mark van Aalst
- Matthew Kenny
- Molten Core
- Project Lifecycle
- Sean Kearney
- Sebastian Patten
- Sitecore Australia
- Sitecore Blog
- Sitecore Climber
- Sitecore Development
- Sitecore Gadgets
- Techphoria414
- The Client View
- The Sitecore Experience
- Web Content Management and Delivery
Archives
- April 2013 (1)
- February 2013 (1)
- January 2013 (1)
- December 2012 (1)
- June 2012 (2)
- May 2012 (2)
- March 2012 (1)
- February 2012 (1)
- January 2012 (5)
- December 2011 (4)
- November 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (2)
- March 2011 (6)
- February 2011 (2)
- January 2011 (10)

Posted under: