Last active
September 5, 2018 06:45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Initialized += Bootstrapper_Initialized; | |
} | |
void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e) | |
{ | |
if (e.CommandName == "Bootstrapped") | |
{ | |
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)); | |
} | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment