Customize RSS webpart

Source: Internet
Author: User

Here are the steps and the code snippet for creating a custom webpart to display items or news via RSS feed.

1. Open the Vs and add a new blank solution.
2. Now in your solution say"Rssfeedpackage"Add a new item then choose Add a webpart. Lets name itRssfeedwebpart.
Please note: You can directly create a webpart solution and do not necessarily have to add it as an item. I did this because I wanted to add some more features in the same solution.
3. next, you need to add some logic to the rsswebpart webpart. edit the rsswebpart. CS class to include the code below. I have added a custom property to receive the RSS feed URL for the webpart.

Code goes like this:

Using system;
Using system. componentmodel;
Using system. runtime. interopservices;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. webcontrols;

Namespace rssfeedpackage
{

[Toolboxitemattribute (false)]
Public class rssfeedwebpart: webpart
{

[Webbrowsable (true)] [personalizable (personalizationscope. Shared)]
Public String rssurl
{
Get; set;
}

Protected override void rendercontents (htmltextwriter writer)
{

Rssfeed feed = new rssfeed (rssurl );
Hyperlink newlink = new hyperlink ();

Foreach (rssitem singlerssitem in feed)
{
Newlink. Text = singlerssitem. title;
Newlink. navigateurl = singlerssitem. href;
Newlink. Target = "rsssite ";
Newlink. rendercontrol (writer );
Writer. writebreak ();
}
Base. rendercontents (writer );
}
}
}

// Code for rssfeed class

Internal class rssfeed: List
{
Internal rssfeed (string rssurl)
{
Try
{
Xmldocument rssdoc = new xmldocument ();
Xmltextreader xread = new xmltextreader (rssurl );
Rssdoc. Load (xread );
Xmlnodelist xnodes = rssdoc. selectnodes ("./RSS/channel/item ");
Foreach (xmlnode xnode in xnodes)
{
This. Add (New rssitem (xnode ));
}
}
Catch (exception)
{
This. Add (New rssitem ());
}
}
}

// Code for rssitem

Internal class rssitem
{
Public String title {Get; internal set ;}
Public String href {Get; internal set ;}
Internal rssitem ()
{
Title = "feed not available at this time ";
Href = "~" ;
}
Internal rssitem (xmlnode xnode)
{
Title = xnode. selectsinglenode ("./Title"). innertext;
Href = xnode. selectsinglenode ("./Link"). innertext;
}
}

4. Now build and deploy the webpart.

5. Finally, insert the webpart in your webpart page and specify the rssurl in edit this webpart-> rssurl box.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.