Wednesday, January 20, 2016

Adding Meta Tags Dynamically through a Web Part in Sharepoint 2013

For dynamic page facebook sharing is not picking up the dynamic content and image. I tried with adding the Open Graph Tags with the Jquery on the sharepoint master page in head section. But it didn't worked out. I have gone through lot of articles then finally i found the solution for the dynamic page you need to add the Open Graph Tags via server side code.

But there was a challenge how to add the Open Graph Meta tags in the Sharepoint Master page through the Visual Web part.

Tried lot of ways and finally got the solution for it.

 In the Master page you need to add a Content Place Holder in the Head section













Then by visual web part you need to access the Content Place Holder

/* Add Open Graph Meta tag in master page*/
System.Web.UI.Page pge = (System.Web.UI.Page)this.Parent.Page;
MasterPage page = pge.Master;
HtmlHead header = page.Page.Header;
System.Web.UI.WebControls.ContentPlaceHolder holder =     (System.Web.UI.WebControls.ContentPlaceHolder)header.FindControl("head");
 HtmlMeta mKey = new HtmlMeta();
 mKey.Name = "og:title";
 mKey.Content = "Tilte Set by Code";
holder.Controls.Add(mKey);

This will add the Title as a Meta tag in the master page. You can add the other properties using the same way








No comments:

Post a Comment