CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

Brand your CS Feeds with the RSS 2.0 image Tag

If you subscribe to a CodeBetter.Com blog with Bloglines, for example, you may have noticed that we’ve added an RSS 2.0 image tag to our feeds.

This involved just a few lines of code, all added to Community Server’s $\Components\Syndication\BaseRssWriter.cs class. Here's the step-by-step if you want to do it yourself.

Step 1: Add an AddImageElement Method.

Add this code somewhere in the BaseRssWriter class:

    /// <summary>

    /// ADDs the image element.

    /// </summary>

    /// <param name="link">Link.</param>

    /// <param name="description">Description.</param>

    protected void AddImageElement(string link, string description)

    {

      // Image Example

      //<image>

      //  <title>Joel On Software</title>

      //  <url>http://www.joelonsoftware.com/RssJoelOnSoftware.jpg</url>

      //  <link>http://www.joelonsoftware.com</link>

      //  <width>144</width>

      //  <height>25</height>

      //  <description>Painless Software Management</description>

      //</image>

      this.WriteStartElement("image");

      this.WriteElementString("title",Title);

      this.WriteElementString("url", BaseUrl + "/RSS2Image.gif");

      this.WriteElementString("link", FormatUrl(link));

      this.WriteElementString("width", "144");

      this.WriteElementString("height", "25");

      this.WriteElementString("description", description);

 

      this.WriteEndElement();

    }

Step 2: Add Calling Code to the BuildChannel Method.

Add the bolded statement below after the “generator” element in the BuildChannel method.

    protected void BuildChannel(string link, string description, string lang)

    {

      this.WriteElementString("title", Title);     

      this.WriteElementString("link", FormatUrl(link));

      this.WriteElementString("description", description);

      this.WriteElementString("dc:language", lang);

      this.WriteElementString("generator", SiteStatistics.CommunityServerVersionVersionInfo);

 

      this.AddImageElement(link, description);

    }

Step 3 Create your RSS Image:

Create a 144 x 25 pixel gif image, and place it in the root of your Community Server Site.

Okay, if you’ve looked at the code above and thought to yourself, “What if my image is a different size?” or “What if I want a different image for each blog?” or “What if I want to use a JPEG or PNG image instead?” 

Well, then you’ve got some code to write!  For now, this was a quick fix, and I only wanted to add one image to all feeds so I didn’t make it configurable.  You should be able to add these as configuration properties, but this will involve additional coding.  If you only want one image for all feeds, my code will work fine for you.

That’s it! 

-Brendan



Comments

darrell said:

Awesome. Great job Brendan!
# April 7, 2005 7:58 AM

Sahil Malik said:

I noticed that this morning. That's hella kool !!
# April 7, 2005 9:20 AM

Brent said:

Brendan,

Nice post, how did you insert your code snipets into this post?
# April 9, 2005 6:14 AM

vern said:

Brendan,
I am interested in using your code (or a semblance of it) in another project, Subtext. What license and/or copyright does this code fall under?

Please email me: vgblog AT the gill family DOT us

THANKS!
# May 8, 2005 2:38 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

Free Tech Publications