Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Blog integration into ASP.NET website using GData API

0.00/5 (No votes)
16 Jul 2012 1  
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.

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.

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.

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.

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: 

 

History

  • Initial post 7/15/2012.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here