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"; if (numberOfEntries <= 0)
numberOfEntries = 10;
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)
{
FeedQuery query = new FeedQuery();
query.Uri = new Uri("http://www.blogger.com/feeds/default/blogs");
AtomFeed feed = service.Query(query);
Uri blogPostUri = null;
if (feed != null)
{
foreach (AtomEntry entry in feed.Entries)
{
if (entry.AlternateUri.Content.Contains(blogname))
{
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)
{
FeedQuery query = new FeedQuery();
query.Uri = blogUri;
query.NumberToRetrieve = numberOfEntries; 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
- The bin directory should contain the following supporting DLLs:
- Google.GData.Blogger.dll
- Google.GData.Client.dll
- You web.config should have following entries for the URL to the blogsite and the credentials:
- blogName
- blogUserid
- blogPass
- noOfEntries
- 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