Use the IPagePreRenderCompleteEvent
You can manipulate the ASP.NET Page object, for example to add meta-attributes to the head tag, using the IPagePreRenderCompleteEvent.
The following code sample demonstrates how to use the IPagePreRenderCompleteEvent:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.HtmlControls;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Web.Events;
namespace SitefinityWebApp
{
public class Global : System.Web.HttpApplication
{
public class FacebookMeta : HtmlMeta
{
public FacebookMeta(string propName, string content)
: base()
{
this.Attributes.Add("property", propName);
this.Content = content;
}
}
protected void Application_Start(object sender, EventArgs e)
{
Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
}
private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
{
EventHub.Subscribe<IPagePreRenderCompleteEvent>((x) =>
{
if (!x.PageSiteNode.IsBackend)
{
var page = x.Page;
var siteNode = x.PageSiteNode;
//add custom class to body tag
var customClass = siteNode.GetCustomFieldValue("BodyClass");
var script = @"$(""body"").addClass(""{0}"");".Arrange(customClass);
page.ClientScript.RegisterStartupScript(typeof(Global), "custom", script, true);
//facebook meta graph
page.Header.Controls.Add(new FacebookMeta("og:title", siteNode.Title));
page.Header.Controls.Add(new FacebookMeta("og:site_name", "yourite.com"));
page.Header.Controls.Add(new FacebookMeta("og:description", siteNode.Description));
var images = siteNode.GetCustomFieldValue("Image") as IList<IDataItem>;
var image = images.FirstOrDefault() as Telerik.Sitefinity.Libraries.Model.Image;
if (image == null)
page.Header.Controls.Add(new FacebookMeta("og:image", ""));
else
page.Header.Controls.Add(new FacebookMeta("og:image", image.Url));
}
});
}
}
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.