Programmatically adding RSSAggregatorWebPart to a Page

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 = "https://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 = "https://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&#91;FeedUrl&#93;\"/>";
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&#91;FeedUrl&#93;"/>
Advertisements

11 thoughts on “Programmatically adding RSSAggregatorWebPart to a Page

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

  2. 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.

  3. This works too:


    RSSAggregatorWebPart rssViewer = new RSSAggregatorWebPart()
    {
    ID = "rssViewer",
    FeedUrl = "https://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);

  4. Getting the following error:

    Could not load type ‘Microsoft.SharePoint.Portal.WebControls.StringResourceManager’ from assembly ‘Microsoft.Office.Server.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’.

    • The example is for MOSS 2007, I see you have 14.0.0.0 version DLLs I am not sure if the blog post applies to SP2010 also

  5. it seems in SharePoint 2013 the webpart type when you add manually is from class XsltListViewWebPart but here we’re trying to add class RSSAggregatorWebPart. I could never seem to get any combination of the code above to work. It seems to me SP2013 requires XsltListViewWebPart but I am never able to supply my Feedurl because there are no feedurl properties in the XsltListViewWebPart class but there is in the RSSAggregatorWebPart class. The problem is whenever I add this webpart, I still get the error. I used the suggestions above but it didn’t work for me. The only difference I can tell is the webparts do not match the same object found in the webpartmanager.webparts.WebBrowsableObject .. Any help would be sorely appreciated. larryhu6 @ gmail . com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s