Get post title & description

Topics: ASP.NET 2.0, Controls
Feb 12, 2013 at 5:11 PM
Hello,

I'm trying to exit the Facebook Thumbnail OG extension by AM22Tech.com to also include the og:title & og:description

I have already added the og:type by adding
HtmlMeta metaTagTypForFacebook = new HtmlMeta();
    metaTagTypForFacebook.Attributes.Add("property", "og:type");
    metaTagTypForFacebook.Attributes.Add("content", "website");
    page.Header.Controls.Add(metaTagTypForFacebook);
My question is, how do I get the title & description of the current post?
I have tried Page.Title & page.MetaDescription
Neither of these work

Thank you
Coordinator
Feb 12, 2013 at 9:12 PM
In the extension constructor, subscribe to the post serving event:
Post.Serving += PostServing;
Add method to run on post serving:
private static void PostServing(object sender, ServingEventArgs e)
{
    var post = (Post)sender;
    var title = post.Title;
    var desc = post.Description.
}
Now you have "post" object and can use any properties, like title or description.
Apr 4, 2013 at 10:55 PM
rtur
i have used your instructions Facebook Thumbnail OG extension by AM22Tech.com for include title and description but don't work.... perhaps i don't understand...

what i must to do? i must to create this rows?

metaTagTitleForFacebook.Attributes.Add("property", "og:title");
metaTagTitleForFacebook.Attributes.Add("content", title);
page.Header.Controls.Add(metaTagTitleForFacebook);

or i must to put in head of my post <meta property="og:title" content="@title">

can you help me???

can you make an example ?

thank you
fabry
Apr 5, 2013 at 9:41 PM
Hi fabry,
I use this code for Facebook
/************************************************************************************/
// Created by :  AM22Tech.com
// Website    :  http://am22tech.com
// Author     :  Soan
/************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Linq;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core.Web.Extensions;
using System.Web.UI.HtmlControls; 

/// <summary>
/// Summary description for FBThumbNail_AM22
/// </summary>
[Extension("Adds an open group tag to first image in your post for FB identification. FB and other sites will show this image as a thumbnail when shared on their platform", "1.0", "<a href=\"http://am22tech.com\">Am22tech.com</a>")]
public class FBThumbNail_AM22
{
    /// <summary>
    /// static constructor to init the settings
    /// </summary>
    public FBThumbNail_AM22()
    {
        Post.Serving += AddFbImageThumbnail;
        BlogEngine.Core.Page.Serving += AddFbImageThumbnail;
    }

    /// <summary>
    /// On Post Served event handler will post to twitter
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void AddFbImageThumbnail(object sender, ServingEventArgs e)
    {
        if (e.Location == ServingLocation.SinglePage)
        {
            BlogEngine.Core.Page p = (BlogEngine.Core.Page)sender;
            AddMetaData("og:title", p.Title + " | Sandro Ego Website");
            AddMetaData("og:type", "website");
            AddMetaData("og:url", p.AbsoluteLink.AbsoluteUri.ToLower());
            AddMetaData("og:description", p.Description);
            string imgPath = string.Empty;
            HttpContext context = HttpContext.Current;

            if (context.CurrentHandler is System.Web.UI.Page)
            {
                System.Web.UI.Page page = (System.Web.UI.Page)context.CurrentHandler;
                imgPath = getImage(true, p.Content);

                if (!imgPath.ToLower().Contains("http://"))
                {
                    imgPath = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + imgPath;
                }

                AddHeader(page, imgPath);
            }

        }

        if (e.Location == ServingLocation.SinglePost)
        {
            Post p = (Post)sender;
            AddMetaData("og:title", p.Title + " | Sandro Ego Website");
            AddMetaData("og:description", p.Description);
            AddMetaData("og:url", p.AbsoluteLink.AbsoluteUri.ToLower());
            AddMetaData("article:author", p.Author);

            string imgPath = string.Empty;
            HttpContext context = HttpContext.Current;

            if (context.CurrentHandler is System.Web.UI.Page)
            {
                System.Web.UI.Page page = (System.Web.UI.Page)context.CurrentHandler;
                imgPath = getImage(true, p.Content);

                if (!imgPath.ToLower().Contains("http://"))
                {
                    imgPath = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + imgPath;
                }

                AddHeader(page, imgPath);
            }
        }
    }

    private void AddHeader(System.Web.UI.Page page, string imgPath)
    {
        //Add meta tag for an appropriate image for facebook - Start   
        // FULL path(URL) of the image on site should be specified, relative path won't work. 
        
        HtmlMeta metaTagImgForFacebook = new HtmlMeta(); 
        metaTagImgForFacebook.Attributes.Add("property", "og:image");
        metaTagImgForFacebook.Attributes.Add("content", imgPath); 
        page.Header.Controls.Add(metaTagImgForFacebook);   
               
        //Add meta tag for an appropriate image for facebook -End 

    }
    private static void AddMetaData(string propertyName, string content)
    {
        if (string.IsNullOrEmpty(propertyName) || string.IsNullOrEmpty(content))
            return;
        if (HttpContext.Current.CurrentHandler is System.Web.UI.Page)
        {
            System.Web.UI.Page pg = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
            HtmlMeta metatag = new HtmlMeta();
            metatag.Attributes.Add("property", propertyName);
            metatag.Attributes.Add("content", content);
            pg.Header.Controls.Add(metatag);
        }
    }

    public string getImage(bool ShowExcerpt, string input) 
    { 
        if (!ShowExcerpt || input == null)
            return string.Empty; 
        string pain = input; 
        string pattern = @"<img(.|\n)+?>"; 
        System.Text.RegularExpressions.Match m = 
            System.Text.RegularExpressions.Regex.Match(input, pattern, 
            System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline); 
        if (m.Success) 
        { 
            string src = getSrc(m.Value); 
            return src;
        } 
        else 
        { 
            return ""; 
        } 
    }
    
    string getSrc(string input) 
    { 
        string pattern = "src=[\'|\"](.+?)[\'|\"]"; 
        System.Text.RegularExpressions.Regex reImg = new System.Text.RegularExpressions.Regex(pattern,
            System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline); 
        System.Text.RegularExpressions.Match mImg = reImg.Match(input); 
        if (mImg.Success) 
        {
            return mImg.Value.Replace("src=", "").Replace("\"", ""); ; 
        }

        return string.Empty; 
     } 
}
/************************************************************************************/
// Created by :  AM22Tech.com
// Website    :  http://am22tech.com
// Author     :  Soan
/************************************************************************************/
Apr 5, 2013 at 9:43 PM
If you are interested, here is the code for Google+ (based on AM22 work)
/************************************************************************************/
// Created by :  IlDrago.com
// Website    :  http://ildrago.com
// Author     :  Alessio
/************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Linq;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core.Web.Extensions;
using System.Web.UI.HtmlControls; 

/// <summary>
/// Summary description for GPlusThumbNail
/// </summary>
[Extension("Adds an open group tag to first image in your post for G+ identification. G+ and other sites will show this image as a thumbnail when shared on their platform", "1.0", "<a href=\"http://am22tech.com\">Am22tech.com</a>")]
public class GPlusThumb
{
    /// <summary>
    /// static constructor to init the settings
    /// </summary>
    public GPlusThumb()
    {
        Post.Serving += AddGPImageThumbnail;
        BlogEngine.Core.Page.Serving += AddGPImageThumbnail;
    }

    /// <summary>
    /// On Post Served event handler will post to twitter
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void AddGPImageThumbnail(object sender, ServingEventArgs e)
    {
        if (e.Location == ServingLocation.SinglePage)
        {
            BlogEngine.Core.Page p = (BlogEngine.Core.Page)sender;
            AddMetaData("name", p.Title + " | Sandro Ego Website");
            AddMetaData("description", p.Description);
            AddMetaData("url", p.AbsoluteLink.AbsoluteUri.ToLower());

            string imgPath = string.Empty;
            HttpContext context = HttpContext.Current;

            if (context.CurrentHandler is System.Web.UI.Page)
            {
                System.Web.UI.Page page = (System.Web.UI.Page)context.CurrentHandler;
                imgPath = getImage(true, p.Content);

                if (!imgPath.ToLower().Contains("http://"))
                {
                    imgPath = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + imgPath;
                }

                AddHeader(page, imgPath);
            }
        }
        if (e.Location == ServingLocation.SinglePost)
        {
            Post p = (Post)sender;
            AddMetaData("name", p.Title);
            AddMetaData("description", p.Description);
            AddMetaData("url", p.AbsoluteLink.AbsoluteUri.ToLower());
            AddMetaData("author", p.Author);

            string imgPath = string.Empty;
            HttpContext context = HttpContext.Current;

            if (context.CurrentHandler is System.Web.UI.Page)
            {
                System.Web.UI.Page page = (System.Web.UI.Page)context.CurrentHandler;
                imgPath = getImage(true, p.Content);

                if (!imgPath.ToLower().Contains("http://"))
                {
                    imgPath = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + imgPath;
                }

                AddHeader(page, imgPath);
            }
        }
    }

    private void AddHeader(System.Web.UI.Page page, string imgPath)
    {
        //Add meta tag for an appropriate image for google - Start   
        // FULL path(URL) of the image on site should be specified, relative path won't work. 
        
        HtmlMeta metaTagImgForGoogle = new HtmlMeta();
        metaTagImgForGoogle.Attributes.Add("property", "image");
        metaTagImgForGoogle.Attributes.Add("content", imgPath); 
        page.Header.Controls.Add(metaTagImgForGoogle);   
               
        //Add meta tag for an appropriate image for facebook -End 

    }
    private static void AddMetaData(string propertyName, string content)
    {
        if (string.IsNullOrEmpty(propertyName) || string.IsNullOrEmpty(content))
            return;
        if (HttpContext.Current.CurrentHandler is System.Web.UI.Page)
        {
            System.Web.UI.Page pg = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
            HtmlMeta metatag = new HtmlMeta();
            metatag.Attributes.Add("itemprop", propertyName);
            metatag.Attributes.Add("content", content);
            pg.Header.Controls.Add(metatag);
        }
    }

    public string getImage(bool ShowExcerpt, string input) 
    { 
        if (!ShowExcerpt || input == null)
            return string.Empty; 
        string pain = input; 
        string pattern = @"<img(.|\n)+?>"; 
        System.Text.RegularExpressions.Match m = 
            System.Text.RegularExpressions.Regex.Match(input, pattern, 
            System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline); 
        if (m.Success) 
        { 
            string src = getSrc(m.Value); 
            return src;
        } 
        else 
        { 
            return ""; 
        } 
    }
    
    string getSrc(string input) 
    { 
        string pattern = "src=[\'|\"](.+?)[\'|\"]"; 
        System.Text.RegularExpressions.Regex reImg = new System.Text.RegularExpressions.Regex(pattern,
            System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline); 
        System.Text.RegularExpressions.Match mImg = reImg.Match(input); 
        if (mImg.Success) 
        {
            return mImg.Value.Replace("src=", "").Replace("\"", ""); ; 
        }

        return string.Empty; 
     } 
}
of course this file must have a different filename, mine is 'GplusThumb.cs' and must be copied under App_Code\Extensions.
Have a nice day!
Apr 7, 2013 at 9:20 PM
Great, work perfectly...

thank you very very much...

Bye
Fabry