开发者

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!


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");
                }
            }

        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜