Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#

Get the RSS URL for an SPList or SPDocumentLibrary Programmatically

Rate me:
Please Sign up or sign in to vote.
1.50/5 (3 votes)
9 Feb 2008CPOL2 min read 36.2K   9   1
Get the RSS URL for a list or document library programmatically.

Background

Recently, I developed an RSS Reader Web Part which would take the RSS URL of a list to render its feeds. Very much like the out of the box Web Part, with the exception that it was AJAX enabled. I would post the details when I am finished.

An idea came to me that, wouldn’t it be nice to also allow users to just give the list URL rather than the RSS URL since that would reduce a few steps on the user’s side. I thought it would be just a matter of accessing the RssUrl property of the SPList object, but to my surprise, it was not to be. There is no such property in the API, so I decided to write my own function for it.

Let’s analyze the RSS URL of a list or a library. Whenever the user clicks on View RSS feed on a library, here is how SharePoint constructs the URL:

http://<server>/<site>/
   _layouts/listfeed.aspx?List=%7B14206B18%2DF68F%2D479B%2DBC84%2D15EE48D19D7D%7D

Listfeed.aspx is the inbuilt RSS viewer of SharePoint which accepts a parameter which is the GUID of the list. The %2D tokens refer to "-" characters which exist inside the GUID. Considering all this, it’s easy to write a function which will return the RSS URL. Here is the code for the same:

C#
private string MakeRssUrl(string rawurl)
{
    try
    {
        Uri url = new Uri(rawurl, UriKind.Absolute);
        if (url.GetLeftPart(UriPartial.Path).Contains("_layouts/listfeed.aspx"))
        {
            return rawurl;
        }
        else
        {
             string rssurl;

             using (SPWeb web = new SPSite(rawurl).OpenWeb())
             {
                 SPList list = web.GetListFromUrl(rawurl);
                 rssurl = web.Url + "/_layouts/listfeed.aspx?list=" + list.ID;
                 return rssurl;
             }
        }
    }
    catch (UriFormatException e)
    {
        return string.Empty;
    }
}

The code is pretty self explanatory. The argument to the function is list URL or RSS URL. We first check if the URL is the RSS URL itself, and if it is, we just return. Otherwise, if it’s a list URL, we create an SPList instance, grab the GUID, and concatenate it with the site URL and listfeed.aspx.

Note that the function does not validate if the given URL is actually a valid list URL or not. The exception handling for that case should be left to the caller of the function.

License

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


Written By
Software Developer (Senior) Wipro Technologies
India India
I am working in Wipro Technologies as a developer with expertises in Microsoft Office SharePoint products. My interests include working on ASP.NET, AJAX, Javascript Object Notation (JSON), XML web services, Algorithm Optimization, Design Patterns.

Comments and Discussions

 
QuestionUsing RSS URL Pin
Member 1098397131-Jul-14 7:33
Member 1098397131-Jul-14 7:33 

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.