Sitefinity: Rss Feeds for Generic Content – Part 3: Podcasts!

 To wrap up this series of posts, I’m going to briefly show you how to extend the Rss Feed Classes to enable support for podcasts. This is the exact same process I took to create my own Dark Synthesis Podcast, which is publishing entries from the the Generic Content Module.

The Generic Content module doesn’t have native support for uploading or associating files. This isn’t such a big deal for me anyway, however, since my files are hosted on Boxstr. So I figured the best way to accomplish this was to store a LINK to the enclosure file as a Meta Field, which I called SongUrl. This is done by adding the meta field to the web.config file:

<add key="Music.SongUrl" valueType="ShortText" visible="True" searchable="False" sortable="False" defaultValue=""/>

Now that this is in place, we basically we need to make changes in three places.

Rss Settings User Control

If you recall, this is the ascx User Control where we have the input fields for setting and updating settings for the Rss feed. We simply need to include an input field for the SongUrl.

<li> <label>Enclosure Meta Field</label> <asp:TextBox runat="server" ID="EnclosureMetaField" ValidationGroup="feedSettings" Columns="15" /></li> 

I decided to name this EnclosureMetaField, in case I (or anyone who might use this) chooses a different name for the Meta Field.

RssSettings Class

Since we added an input field to the Rss Settings User Control, we also need to make sure that we access this in our RssSettings class, specifically we need a reference to it in the ControlContainer class:

public IEditableTextControl EnclosureMetaField
{
    get {
        if (enclosureMetaField == null)
            enclosureMetaField = (IEditableTextControl)base.FindControl(typeof(IEditableTextControl), "EnclosureMetaField", true);

        return enclosureMetaField;
    }
}

We also need to make sure that we handle loading the data in the CreateChildControls method:

this.ctrlContainer.EnclosureMetaField.Text = this.settings["EnclosureMetaField"];

as well as saving the settings data in the SaveSettings method:

settings["EnclosureMetaField"] = this.ctrlContainer.EnclosureMetaField.Text;

Now we move on to the last piece of the puzzle.

RssChannelProvider

The last thing we need to do is use this meta field to create the enclosure. Luckily, the RssItem class already has support for enclosures, we just need to initialize it with the correct data. First we need to make sure that we retrieve this field from the settings:

private string EnclosureMetaField; public override void Initialize(IDictionary<string, string> settings)
{
    base.Initialize(settings);
    EnclosureMetaField = settings["EnclosureMetaField"];
    Category = settings["Category"];
    ItemCount = Int32.Parse(settings["ItemCount"]);
}

finally, we override the SetGeneralProps method and initialize and populate the enclosure property of the RssItem class. Note that we need to retrieve both the size and mime type of the item, which we do by calling the headers from the file using a WebRequest.

protected override void SetGeneralProps(RssItem item, IContent content)
    {
        if (content.GetMetaData(EnclosureMetaField) != null)
        {
            
            item.Enclosure = new RssEnclosure();
            item.Enclosure.Url = new Uri(content.GetMetaData(EnclosureMetaField).ToString());
            WebRequest req = WebRequest.Create(item.Enclosure.Url);
            req.Method = "HEAD";

            item.Enclosure.Length = Convert.ToInt32(req.GetResponse().ContentLength);
            item.Enclosure.Type = "audio/mpeg";
        }
        base.SetGeneralProps(item, content);
    }

And that is literally it! In my case, I know the files will always be mp3s, so I hard coded the mime type to avoid a second request (for some reason, I cannot retrieve both Length and Type in one call. I’m sure this can be fixed, I was just too lazy).

So there you have it! Custom rss feeds for the Generic Content module, including podcast/enclosure support! Now that you know how to do it, get out there and do it! and be sure to subscribe to my podcast. If you enjoy it, please consider donating your support to my shows host, Sanctuary Radio.

Thanks for reading, til next time!