Social Media Share buttons - BlogEngine.NET Extension

Posted on Tuesday, January 29, 2013 by Krish Tags: , ,
Category: ASP.NET, C#, Bloging, Programing

Social Media Share buttons - BlogEngine.NET Extension

We have just completed the Upgrading of our blogging platform to the latest BlogEngine.NEt version 2.7.0.0.

I just updated an Extension which I wrote earlier to display the share buttons for Google Plus, Twitter and Facebook to include the Pinterest. So now the extension will display 4 buttons below your post just like the above page.

So if anyone want this extension, just leave a comment, or Save the following code as a class (.cs) file and move it to \App_Code\Extensions

Don’t forget to change the twitterUser  string to your twitter username

using System;
using System.Text;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;

/// <summary>
/// Summary description for retweet
/// </summary>
///
[Extension("Inserts Tweet, Facebook, Google Plus and Pinterest share buttons", "1.1", "Geekiest.Net")]
public class retweet
{
   
    private static string _Html;  
    private static string twitterUser = "XXXXXXXXXXXXX"; // Your twitter user name will be used in the retweet

    /// <summary>
    /// Gets the HTML used to inserts the AdSense code.
    /// </summary>
    public static string Html
    {
        get
        {
            if (_Html == null)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(getstr());
                _Html = sb.ToString();
            }
            return _Html;
        }
    }

    public retweet()
    {
        Post.Serving += new EventHandler<ServingEventArgs>(Serving);
        Page.Serving += new EventHandler<ServingEventArgs>(Serving);
    }

    /// <summary>
    /// Serving AdSense code in post and page
    /// </summary>
    private static void Serving(object sender, ServingEventArgs e)
    {
        if ((e.Location == ServingLocation.SinglePost))
        {
            string _adScript = Html;
            Post post = (Post)sender;
            e.Body += _adScript;
            string longUrl=System.Web.HttpUtility.UrlEncode(post.AbsoluteLink.ToString());
            e.Body = e.Body.Replace("http%3a%2f%2fgeekiest.net%2fpost%2fSocial-Media-Share-buttons-BlogEngineNET-Extension.aspx",longUrl);
            e.Body = e.Body.Replace("Social Media Share buttons - BlogEngine.NET Extension", post.Title);
            string img = getImage(e.Body);
            e.Body = e.Body.Replace("http%3a%2f%2fgeekiest.net%2fimage.axd%3fthumb%3dy%26picture%3dSocial_Media_Share_buttons_-_BlogEngine.NET_Extension.png", System.Web.HttpUtility.UrlEncode(img));
           
        }
        if (e.Location == ServingLocation.SinglePage)
        {
            string _adScript = Html;
            Page page = (Page)sender;
            e.Body += _adScript;
            string longUrl = System.Web.HttpUtility.UrlEncode(page.AbsoluteLink.ToString());
            e.Body = e.Body.Replace("http%3a%2f%2fgeekiest.net%2fpost%2fSocial-Media-Share-buttons-BlogEngineNET-Extension.aspx", longUrl);
            e.Body = e.Body.Replace("Social Media Share buttons - BlogEngine.NET Extension", page.Title);
            string img = getImage(e.Body);
            e.Body = e.Body.Replace("http%3a%2f%2fgeekiest.net%2fimage.axd%3fthumb%3dy%26picture%3dSocial_Media_Share_buttons_-_BlogEngine.NET_Extension.png", System.Web.HttpUtility.UrlEncode(img));
        }
    }


Advertisement


    private static string getstr()
    {
        string strr = "<div style=\"clear: both;\"></div>"; 
        strr += "<div style=\"float:left\"><script type=\"text/javascript\" src=\"http://apis.google.com/js/plusone.js\"></script> <g:plusone></g:plusone></div>";
        strr += "<div style=\"float:left\"><a href=\"http://twitter.com/share\" class=\"twitter-share-button\" data-count=\"horizontal\" data-via=\""+twitterUser+"\">Tweet</a><script type=\"text/javascript\" src=\"http://platform.twitter.com/widgets.js\"></script></div>";
        strr += "<div style=\"float:left;width:80px\"><a href=\"http://pinterest.com/pin/create/button/?url=http%3a%2f%2fgeekiest.net%2fpost%2fSocial-Media-Share-buttons-BlogEngineNET-Extension.aspx&media=http%3a%2f%2fgeekiest.net%2fimage.axd%3fthumb%3dy%26picture%3dSocial_Media_Share_buttons_-_BlogEngine.NET_Extension.png&description=Social Media Share buttons - BlogEngine.NET Extension\" class=\"pin-it-button\" count-layout=\"horizontal\"><img border=\"0\" src=\"//assets.pinterest.com/images/PinExt.png\" title=\"Pin It\" /></a></div>";
        strr += "<div style=\"float:left\"><iframe src=\"http://www.facebook.com/plugins/like.php?app_id=226294280715030&amp;href=http%3a%2f%2fgeekiest.net%2fpost%2fSocial-Media-Share-buttons-BlogEngineNET-Extension.aspx&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:100px; height:21px;\" allowTransparency=\"true\"></iframe></div>";
         //strr += "</tr></table>";
        strr += "<div style=\"clear: both;\"></div>";
        strr = strr.Replace("\r\n", "");
        strr = strr.Replace("\n", "");
        return (strr);

    }

 

    private static string getImage(string input)
    {

        if (input == null)
            return "";

        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);
            src=src.Replace("src=", "").Replace("\"", "").Replace("\\", "");
            return src;
        }
        else
        {
            return "";
        }
    }

 

    private static 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)
        {
            string a = mImg.Value; 
            return a;
        }

        return "";
    }


}

You are free to Edit/Share with or with out credits Smile




Christmas Giveaway - Free WinX DVD Copy Pro full version license

Christmas Giveaway - Free WinX DVD Copy Pro full version license

Christmas Giveaway - Free WinX DVD Copy Pro full version license

WinX DVD Copy Pro can convert DVDs to another formats like ISO, MPEG etc to backup your DVDs. It can also help you to copy a DVD into another DVD.

WinX DVD Copy Pro is specially designed to meet users' up-to-date DVD backup demand: clone DVD to DVD disc for safe storage; copy DVD to ISO image for later burning, playing or ripping; copy DVD to MPEG2 file with intact content for further usage in media center, DVD library establishment and DVD playback in PS3, HTPC. Equipped with ISO mounter and DVD burner, this DVD copy software can also help mount ISO to a virtual drive and burn DVD to DVD.


Advertisement


Features

Full DVD Copy with 1:1 Ratio

Clone DVD to ISO image

Backup DVD to VIDEO_TS folder

Extract Video / Audio

DVD Burner & ISO Mounter

Repair Corrupted/ARccOS Bad Sectors

Read more about WinX DVD Copy Pro here

You can get a free full version license of WinX DVD Copy Pro free from this promotional page