Click here to Skip to main content
15,867,704 members
Articles / Web Development / ASP.NET

Blog integration into ASP.NET website using GData API

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
16 Jul 2012CPOL3 min read 37.9K   2.5K   13   5
Content Managemnt of asp.net website using Blogger Blog and Google GDI API

Introduction

Many websites for individuals and companies consist of mostly static pages but sometimes there is a need to provide updates to customers or information about the company news. Most of these websites would require actual update of the code to publish these news items or updates. For smaller companies a dynamic way to maintain these pages can be integration into blog site where by the contents of the blog is automatically pulled into the page which is to be updated. This provides a way to manage content on these pages without touching the code on the website or doing a deployment. An update to the blog is reflected directly into the target page. This article provides one such implementation of this feature using the blogger website and ASP.NET website. Elsewhere on this site blog integration into ASP.NET blogger is discussed with FTP solution, but this feature is being phased out. My solution involves using the GData service API from Google to query the blog connects on the fly and display them in your ASP.NET website, although there is no reason why it cannot be used in other other types of websites.

Environment

The Incede.BloggerInteg.dll is written in C#3.5 and the sample webpage is designed in VS2008. It should be easily upgradable to VS2010.

Using the code

The project is implemented as a custom DLL (Incede.BloggerInteg.dll), This component has BlogService class with a public method GetUserBlogs() which returns a list the blogs published on the specified blogger site.

The constructor of the BlogService service class takes the blog name, user id, password of the blog as arguments. Also included in the constructor is the number of blog entries to pull. At the core of the component are two API's from Google for access to the blogger system.

  • Google.GData.Blogger
  • Google.GData.Client

These components provide the necessary service of online authentication into the blogger platform and ability to query the user blogs. The GetUserBlogs first creates an instance of the service credentials using the Google GDataCredentials and the service is getting the blog entries using the GDATA and the Blog URI.

C#
public List<blogentry> GetUserBlogs()
{
    if (blogname.Trim().Equals(String.Empty))
        blogname = "incedeit.blogspot.com";//default if not supplied
    if (numberOfEntries <= 0)
        numberOfEntries = 10; //default

    var gService = new Google.GData.Client.Service("blogger", blogname);
    gService.Credentials = new GDataCredentials(username, password);

    Uri blogPostUri = SelectUserBlog(gService, blogname)
    List<blogentry> blogs = GetBlogEntryContent(gService, 
    blogPostUri,   numberOfEntries);
    return blogs;
}

This is the method where the core of the process of identifying the user blogs is performed. An atomfeed is first created from the query and then the entries are filtered down to the user blog. This user blog is identified by a static URI.

C#
static Uri SelectUserBlog(Service service, string blogname)
{
    //blogname = incedeit.blogspot.com example 
    FeedQuery query = new FeedQuery();
    // Retrieving a list of blogs
    query.Uri = new Uri("http://www.blogger.com/feeds/default/blogs");
    AtomFeed feed = service.Query(query);

    // Publishing a blog post
    Uri blogPostUri = null;
    if (feed != null)
    {
        foreach (AtomEntry entry in feed.Entries)
        {
            //iterate thru blogs 

            if (entry.AlternateUri.Content.Contains(blogname))
            {
                // find the href in the link with a rel pointing to the blog's feed
                for (int i = 0; i < entry.Links.Count; i++)
                {
                    if (entry.Links[i].Rel.Equals("http://schemas.google.com/g/2005#post"))
                    {
                        blogPostUri = new Uri(entry.Links[i].HRef.ToString());
                    }
                }

                return blogPostUri;
            }
        }
    }
    return blogPostUri;
}

This method finally iterates through the entries of the user blog URI identified and pulls the relevant content from the blog such as the title, content, and footer.

C#
static List<blogentry> GetBlogEntryContent(Service service, Uri blogUri, int numberOfEntries)
{
    List<blogentry> allblogs = new List<blogentry>();
    if (blogUri != null)
    {

        // Retrieve all posts in a blog
        FeedQuery query = new FeedQuery();
        query.Uri = blogUri;
        query.NumberToRetrieve = numberOfEntries; //get last x number of posts
        AtomFeed feed = service.Query(query);
        foreach (AtomEntry entry in feed.Entries)
        {
            BlogEntry b = new BlogEntry();
            b.Title = entry.Title.Text;
            b.content = entry.Content.Content;
            b.footer = String.Format("Posted on {0}", entry.Published.ToString());
            allblogs.Add(b);
        }
    }
    return allblogs;
}

Usage

To use the component we simply create an instance of the service Incede.BloggerInteg.BlogService and supply it with a few parameters ie the blogname, credentials, and the number of blog entries we are interested in pulling. These parameters are defined in the web.config in our sample project. The service returns a IList of blog entry class with properties such as Blog title, Blog Content and footer.ShowBlogEntries simply formats the blog entries into a data list for display.

C#
Incede.BloggerInteg.BlogService Biservicc = new Incede.BloggerInteg.BlogService(blogname, userid,pwd,int.Parse(entries));
 
List<incede.bloggerinteg.blogentry> blogs =   Biservicc.GetUserBlogs();
ShowBlogEntries(blogs, blogname);

Putting it together

  1. The bin directory should contain the following supporting DLLs:
    • Google.GData.Blogger.dll
    • Google.GData.Client.dll
  2. You web.config should have following entries for the URL to the blogsite and the credentials:
    • blogName
    • blogUserid
    • blogPass
    • noOfEntries
  3. You can modify the core service to return additional parameters from the blogger site if needed. My implementation has the most  basic items that would be needed. 

Sample Project 

I have used a sample blog I created for this project to illustrate the concept in the sample project. The actual blog site is http://incedeblogger.blogspot.com. Any updates to the blog are automatically pulled down into the sample project webpage. A live implementation of this in action can be seem on my website http://www.incedeit.com/main/UI/News.aspx.

Screen Shot of the integrated page: 

Image 1 

History

  • Initial post 7/15/2012.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Lead Enterprise Application development and architecture using Asp.net/C#/Ajax/SSIS with back end databases like Ms-SQL, Oracle.

You can contact me at suhel.shah@gmail.com

Comments and Discussions

 
QuestionIs the method of authentication still work? Pin
Lyubomir Rumenov Velchev9-Nov-15 12:44
Lyubomir Rumenov Velchev9-Nov-15 12:44 
QuestionWhere is GData Pin
krishnananda4222-Nov-14 8:55
krishnananda4222-Nov-14 8:55 
QuestionComprehensive and Smart but complete article on an instance. Pin
ijazuddin jafri18-Nov-12 16:06
ijazuddin jafri18-Nov-12 16:06 
QuestionWhere is GData.Blogger dll Pin
Ravinder Singh Bhawer17-Oct-12 21:40
Ravinder Singh Bhawer17-Oct-12 21:40 
GeneralMy vote of 5 Pin
anaparthiarun25-Jul-12 3:41
anaparthiarun25-Jul-12 3:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.