user715881
user715881

Reputation: 21

Add Facebook Open Graph Protocol Meta Tags to sharepoint

When I like an article on our website (www.potatopro.com) with a Facebook like and a facebook sharebutton the wrong website data is being fetched. Either you are not able to change the picture or in the other case facebook fetches the navigation instead of the content.

To my understanding I have to implement facebook's open graph protocol meta-tags on our site. But how do I do that for a sharepoint based website?! Please advice!

Upvotes: 2

Views: 1883

Answers (1)

Lotta Asplund
Lotta Asplund

Reputation: 41

You can add a webpart to the pagelayout that your page is using. In the webpart you add a function that finds the title, content and image on the page and writes metatags to the masterpage that the page is using. Here is an example of the function...

protected override void Render(HtmlTextWriter writer)
    {
        if (SPContext.Current != null && SPContext.Current.ListItem != null)
        {
            SPListItem item = SPContext.Current.ListItem;
            var title = item["Title"];
            if (title != null)
            {
                writer.WriteBeginTag("meta");
                writer.WriteAttribute("property", "og:title");
                writer.WriteAttribute("content", title.ToString());
                writer.WriteEndTag("meta");
            }
            var pageContent = item["PublishingPageContent"];
            if (pageContent != null)
            {
                string strippedPageContent = Regex.Replace(pageContent.ToString(), @"<(.|\n)*?>", string.Empty);
                    writer.WriteBeginTag("meta");
            writer.WriteAttribute("property", "og:description");
            writer.WriteAttribute("content", strippedPageContent);
            writer.WriteEndTag("meta");
            }

            var pageImage = item["PublishingPageImage"];
            if (pageImage != null)
            {
                ImageFieldValue pageImageValue = pageImage as ImageFieldValue;
                if (pageImageValue != null)
                {
                    var url = pageImageValue.ImageUrl;
                    writer.WriteBeginTag("meta");
                    writer.WriteAttribute("property", "og:image");
                    writer.WriteAttribute("content", url);
                    writer.WriteEndTag("meta");
                }
            }

        }
    }

Upvotes: 3

Related Questions