Last active
January 30, 2020 19:54
-
-
Save deniskyashif/a42718eb57b5d913aa0d to your computer and use it in GitHub Desktop.
Integrate OpenGraph and TwitterCard with Sitefinity
This file contains 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
protected void Application_Start(object sender, EventArgs e) | |
{ | |
Bootstrapper.Initialized += this.BootstrapperInitializedHook; | |
} |
This file contains 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 Telerik.Sitefinity.Abstractions; | |
using Telerik.Sitefinity.Data; | |
using Telerik.Sitefinity.Services; | |
using Telerik.Sitefinity.Web; | |
using Telerik.Sitefinity.Web.Events; | |
public class Global : System.Web.HttpApplication | |
{ | |
protected void Application_Start(object sender, EventArgs e) | |
{ | |
Bootstrapper.Initialized += this.BootstrapperInitializedHook; | |
} | |
private void BootstrapperInitializedHook(object sender, ExecutedEventArgs e) | |
{ | |
if (e.CommandName == "Bootstrapped") | |
{ | |
EventHub.Subscribe<IPagePreRenderCompleteEvent>(evt => | |
{ | |
var page = evt.Page; | |
var siteNode = evt.PageSiteNode; | |
page.Header.Controls.Add(new OpenGraphMeta("og:title", page.Title)); | |
page.Header.Controls.Add(new OpenGraphMeta("og:url", UrlPath.ResolveAbsoluteUrl(siteNode.Url))); | |
page.Header.Controls.Add(new TwitterMeta("twitter:title", page.Title)); | |
// Include additional tags ... | |
}); | |
} | |
} | |
} |
This file contains 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
private void BootstrapperInitializedHook(object sender, ExecutedEventArgs e) | |
{ | |
if (e.CommandName == "Bootstrapped") | |
{ | |
EventHub.Subscribe<IPagePreRenderCompleteEvent>(evt => | |
{ | |
var page = evt.Page; | |
var siteNode = evt.PageSiteNode; | |
page.Header.Controls.Add(new OpenGraphMeta("og:title", page.Title)); | |
page.Header.Controls.Add(new OpenGraphMeta("og:url", UrlPath.ResolveAbsoluteUrl(siteNode.Url))); | |
page.Header.Controls.Add(new TwitterMeta("twitter:title", page.Title)); | |
// ... | |
}); | |
} | |
} |
This file contains 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.Web.UI.HtmlControls; | |
public class OpenGraphMeta : HtmlMeta | |
{ | |
public OpenGraphMeta(string propertyName, string content) : base() | |
{ | |
this.Attributes.Add("property", propertyName); | |
this.Content = content; | |
} | |
} |
This file contains 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.Web.UI.HtmlControls; | |
public class TwitterMeta : HtmlMeta | |
{ | |
public TwitterMeta(string propertyName, string content) : base() | |
{ | |
this.Attributes.Add("name", propertyName); | |
this.Content = content; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment