|
It is obvious that if I had another webpart that could hold my RSS feeds list,
or OPML, and somehow I'd be able to connect these two, I'd have at my hands a
fairly nice online RSS reader.
So in this blogpost, lets go ahead and write up a web part that lets you hold
your OPML. To keep this short and to the point, I am going to skip over the
basics, and provide you with enough links, so you could do the reading
yourself.
• Like before, I setup a business object - RssSubscription, and a List is pretty much all I need to hold my RssSubscriptions. This business object had two properties - FeedName and FeedURL(both strings)
• Next, I set up writing the OPMLWebPart. To get me started, I created two RssSubscriptions, and added them to List rssUrls in the
constructor. Here is the code -
public
OPMLWebpart()
{
rssUrls = new
List<RssSubscription>();
rssUrls.Add(new
RssSubscription("Sahil Malik",
"http://blah.winsmarts.com/rss2.aspx"));
rssUrls.Add(new
RssSubscription(" Events","sitename"
));
}
•
With this private variable setup and populated, my rendering becomes very easy.
I simply create a Radio Button List to allow the user to pick the RSS feed he
wants as shown below, and add that to the Controls collection - thus they would
get rendered automatically.
protected
override void
CreateChildControls()
{
rbl = new
RadioButtonList();
foreach (RssSubscription
feed in rssUrls)
{
rbl.Items.Add(new
ListItem(feed.FeedName, feed.FeedURL));
}
rbl.AutoPostBack = true;
this.Controls.Add(rbl);
base.CreateChildControls();
}
• I also created a public property that is personlizable, so the user can
specify his RSS Feeds.
[Personalizable(true)]
public List<RssSubscription>
RssUrls
{
get {
return rssUrls; }
set { rssUrls =
value; }
}
Now since the above is a complex type, I need a custom editor, rather than a
textbox to allow the user to specify his RSS Feeds in. I am going to only gloss
over implementing the Editor here, for an in depth talk on Editors,
• In order to do so, I create a class which will be my Editor called
OPMLEditor, that inherits from EditorPart.
• I make my OPMLWebPart implement IWebEditable, which requires me to add a
method called CreateEditorPArts and WebBrowsableObject. These are implemented
as shown below.
#region
IWebEditable Members
EditorPartCollection IWebEditable.CreateEditorParts()
{
List<EditorPart>
editors = new List<EditorPart>();
editors.Add(new
OPMLEditor());
return
new EditorPartCollection(editors);
}
object IWebEditable.WebBrowsableObject
{
get {
return this; }
}
#endregion
•
The Editor Part itself, is going to show a simple multiline textbox, in which
the user can type his "FeedName, FeedURL" in a CSV format in a multi-line
editor. The multiline editor is implemented as follows -
protected
override void
CreateChildControls()
{
opmlCSV = new
TextBox();
opmlCSV.TextMode = TextBoxMode.MultiLine;
opmlCSV.Width = new
Unit("300px");
opmlCSV.Height = new
Unit("100px");
Controls.Add(opmlCSV);
}
• The two abstract methods that EditorPart will require you to implement are
implemented as follows.
public
override bool
ApplyChanges()
{
EnsureChildControls();
OPMLWebpart part =
WebPartToEdit as OPMLWebpart;
if (part !=
null)
{
part.OPMLS = opmlCSV.Text;
}
else
{
return
false;
}
return
true;
}
public override
void SyncChanges()
{
EnsureChildControls();
OPMLWebpart part =
WebPartToEdit as OPMLWebpart;
if (part !=
null)
{
opmlCSV.Text = part.OPMLS ;
}
}
•And if you noticed, I just added a new property called OPMLS on the part. It's
implementation simply converts the CSV into List
as shown below.
public
string OPMLS
{
get
{
StringBuilder
sb = new StringBuilder();
foreach
(RssSubscription subscription
in rssUrls)
{
sb.Append(subscription.FeedName);
sb.Append(",");
sb.Append(subscription.FeedURL);
sb.AppendLine();
}
return
sb.ToString();
}
set
{
string
opmls = value;
rssUrls.Clear();
String[]
feeds = opmls.Split('\n');
foreach
(string feed in
feeds)
{
String[] feedParts = feed.Split(',');
RssSubscription subscription = new
RssSubscription(feedParts[0], feedParts[1]);
rssUrls.Add(subscription);
}
}
}
Great!! Compile and build this sucker, deploy it to your ASP.NET 2.0 WebPart
framework based site, and run the website. Go ahead and set the page in
"Catalog", and add the OPML Editor webpart to a WebPart Zone. This is shown as
below.
You can see the two RssSusbcriptions I added in the constructor are
pre-populated. The RSSImport webpart that appears below is what you already saw
in this blogpost.
If you put the page in Edit display mode, you should also see the rudimentary
but functional editor you just wrote in action. This can be seen as below.
Now when you set the page in Browse DisplayMode, you should be able to see the
OPML Editor, and the RSS Rendering side by side as shown below.
What is disconcerting however at this time, is that for me to create an
RSS-Reader, clicking on a Radio button should change the RSS URL of the RSS
Rendering below. While selecting a particular RSS Feed does cause a postback,
it doesn't update the RssUrl of the RSS Rendering Webpart.
This is because, at this time, these two Webparts Do not Communicate with each
other.
So in the next blog post, I will talk about how to make these two web parts
communicate. But for the meanwhile, I'd recommend brushing up on the basics of
WebPart communication by reading -
|