Programmatically adding RSSAggregatorWebPart to a Page

July 3, 2008

I have been struggling to find information on how to add RSSAggregatorWebPart to a page. First I started with doing something like

RSSAggregatorWebPart rssViewer = new RSSAggregatorWebPart();
rssViewer.FeedUrl = "http://ketulpatel.wordpress.com/feed/";
rssViewer.ID = "rssFeed";
rssViewer.DataSourcesString = dataSourceString.ToString();
rssViewer.Title = "My Blog";
//AddWebPart is an helper method to add a webpart to a page
AddWebPart(web,rssViewer,"default.aspx","CenterColTop",0);

That added the web-part to the page but when rendering the Page, web part gave an error asking to check error logs for errors and error log under 12 hive contained following error

RssWebPart: Exception handed to HandleRuntimeException.HandleException System.ArgumentNullException: Value cannot be null. Parameter name: key at Microsoft.SharePoint.WebControls.CacheObject.LoadDataFromCache(String key) at Microsoft.SharePoint.WebControls.BaseXmlDataSource.GetXmlDocument() at Microsoft.SharePoint.WebPartPages.DataFormWebPart.GetHierarchicalDocument(IHierarchicalDataSource ds) at Microsoft.SharePoint.WebPartPages.DataFormWebPart.GetHierarchicalXPathNavigator(IHierarchicalDataSource ds) at Microsoft.SharePoint.WebControls.SingleDataSource.GetXPathNavigatorInternal() at Microsoft.SharePoint.WebControls.SingleDataSource.GetXPathNavigator() at Microsoft.SharePoint.WebControls.SingleDataSource.GetXPathNavigator(IDataSource datasource, Boolean origin…

Blha Blha….. Really could not tell what’s going on, but If I add the same web-part from the UI (Using the Web Part Gallery) than it worked fine and my feed showed up in the web-part. After examining the properties of the working web-part in the debug mode, I noticed the web part has DataSourceString and ParameterBindings property set, MSDN has no documentation about it (or at least I could not find it) and setting those properties when adding RSS WebPart programmatically worked

Here is the full code snippet

RSSAggregatorWebPart rssViewer = new RSSAggregatorWebPart();
rssViewer.FeedUrl = "http://ketulpatel.wordpress.com/feed/";
rssViewer.ID = "rssFeed";
StringBuilder dataSourceString = new StringBuilder("<%@ Register TagPrefix=\"WebControls\" Namespace=\"Microsoft.SharePoint.WebControls\" Assembly=\"Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\" %>");
            dataSourceString.Append("<%@ Register TagPrefix=\"WebPartPages\" Namespace=\"Microsoft.SharePoint.WebPartPages\" Assembly=\"Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\" %>");
            dataSourceString.Append("<WebControls:XmlUrlDataSource runat=\"server\" AuthType=\"None\" HttpMethod=\"GET\">");
            dataSourceString.Append("<DataFileParameters>");
            dataSourceString.Append("<WebPartPages:DataFormParameter Name=\"RequestUrl\" ParameterKey=\"RequestUrl\" PropertyName=\"ParameterValues\"/>");
            dataSourceString.Append("</DataFileParameters>");
            dataSourceString.Append("</WebControls:XmlUrlDataSource>");

rssViewer.DataSourcesString = dataSourceString.ToString();
rssViewer.ParameterBindings = "<ParameterBinding Name=\"RequestUrl\" Location=\"WPProperty[FeedUrl]\"/>";
rssViewer.Title = "My Blog";
AddWebPart(web,rssViewer,"default.aspx","CenterColTop",0);

Value of DataSourceString is:

<%@ Register TagPrefix="WebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<WebControls:XmlUrlDataSource runat="server" AuthType="None" HttpMethod="GET">
<DataFileParameters>
<WebPartPages:DataFormParameter Name="RequestUrl" ParameterKey="RequestUrl" PropertyName="ParameterValues"/>
</DataFileParameters>
</WebControls:XmlUrlDataSource>

Value of ParameterBindings is:

<ParameterBinding Name="RequestUrl" Location="WPProperty[FeedUrl]"/>

Entry Filed under: MOSS. Tags: , .

6 Comments Add your own

  • 1. Mike  |  July 29, 2008 at 2:16 pm

    Thanks for this! I was having the exact same problem this morning. Blogs + Search Engines = Huge Win for programmers.

    Reply
  • 2. Dip  |  April 15, 2009 at 3:40 pm

    What is the difference between the RSSViewerWebPart and RSSAggregatorWebPart. What is the purpose behind having two similar web parts? In my environment, RSSViewer web part is working but not the RSSAggregator web part.

    I populated the RSSAggregatorWebPart from the gallery and then added it on to a page. But when I add a feed url to it, it gives an error “An unexpected error occured processing your request. Check the logs for details and correct the problem.”

    The same feed works fine with a RSSViewer webpart.

    Reply
  • 3. Dennis  |  May 27, 2009 at 12:32 pm

    Thanks, this comment was very helpful. I was running into the same error, until I came across your blog post.

    Problem solved now :)

    Reply
  • 4. snahta  |  June 14, 2009 at 10:05 pm

    This doesn’t work ( compile ) , RSSAggregatorWebPart doesn’t have a DataSourcesString property

    Reply
  • 6. Andrew  |  August 11, 2009 at 1:55 pm

    This works too:


    RSSAggregatorWebPart rssViewer = new RSSAggregatorWebPart()
    {
    ID = "rssViewer",
    FeedUrl = "http://ketulpatel.wordpress.com/feed/",
    FeedLimit = 5,
    ParameterBindings = "",
    Title = "Custom RSS"
    };
    XmlUrlDataSource rssDataSource = new XmlUrlDataSource()
    {
    AuthType = "None",
    HttpMethod = "GET"
    };
    rssDataSource.DataFileParameters.Add(
    new DataFormParameter()
    {
    Name = "RequestUrl",
    ParameterKey = "RequestUrl",
    PropertyName = "ParameterValues"
    });
    rssViewer.DataSources.Add(rssDataSource);

    manager.AddWebPart(rssViewer, "MiddleRightZone", 3);

    Reply

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Tags

Add new tag Commerce Server JavaScript jQuery MOSS MOSS Search WebParts Workflow WPF/E

KWitty

Blogroll

Feeds

Add to Technorati Favorites

del.icio.us

Categories