2. Writing the OPML WebPart and a WebPart Editor
You just wrote the RSSWebPart. The RSS WebPart
required some runtime configuration, specifically you needed to specify
an RSS URL to the RSSWebPart. The WebBrowsable(true) attribute provided
you a convenient way to allow SharePoint to give you an Editing UI for
that property.
Next, you are going to write the OPML WebPart. The
OPML WebPart will host a number of RSS URLs and will present the user
with the RSS URLs laid out with radio buttons next to them. The idea is
that the user can select any of the RSS URLs. In the next section, I
will connect the two WebParts so the selected RSSURL in the OPML editor
is rendered as an RSS feed in the RSSWebPart.
While it was convenient to edit one single RSS feed
URL using a simple text box, editing the OPML WebPart's numerous RSS
Feeds perhaps requires a different UI. To keep things simple instead of
creating a single line text box, I will instead create a multiline text
box allowing the user to enter multiple RSS URLs as comma separated
values. This multiline text box would be the custom editor for the OPML
WebPart.
In this section, you look at the process of writing
yet another WebPart and connecting it to its custom editor. Let's get
started.
Back in your Visual Studio project, add another
WebPart and call it the OPMLWebPart. In the OPMLWebPart.cs file, write
the code shown in Listing 7.
Example 7. The OPMLWebPart Code without the Editor
[ToolboxItemAttribute(false)]
public class OPMLWebPart : WebPart
{
private RadioButtonList rbl;
[Personalizable(PersonalizationScope.Shared)]
public List<String> FeedURLS { get; set; }
public OPMLWebPart()
{
FeedURLS = new List<string>();
}
protected override void CreateChildControls()
{
rbl = new RadioButtonList() { AutoPostBack=true };
foreach (string rssFeed in FeedURLS)
{
rbl.Items.Add(new ListItem(rssFeed));
}
this.Controls.Add(rbl);
}
}
|
If you look at the code in Listing 7,
note that you've created a simple WebPart with the public property
called FeedURLs. What you haven't done though is provide the ability for
the user to edit the value of FeedURLs. The WebBrowsable(true)
attribute is not going to work here for two reasons.
First, the single line text box is not a good UI to
enter multiple RSS Feed URLs, and secondly, List<String> is a
complex object. It is not a simple string, or an enum, so the framework
won't be able to intelligently serialize or deserialize it. In order to
edit the FeedURLs, you have to write a custom editor.
2.1. Writing a Custom Editor
Writing a custom editor for a WebPart is rather
simple and involves two steps. First, your WebPart has to implement the
IWebEditable interface, and second you have to implement a class that
inherits from the EditorPart abstract base class. This inherited class
will be the WebPart editor.
Go ahead and implement the IWebEditable the interface
in the OPMLWebPart. Implementing the IWebEditable interface would
require you to make the change shown in Listing 8 to your OPMLWebPart.
Example 8. IWebEditable Implementation
#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
|
As you can see from Listing 8,
the CreateEditorParts method returns you an EditorPartCollection.
Therefore, numerous editors can work on one single WebPart at the same
time. This makes sense considering that there may be numerous properties
on the WebPart and each of them may acquire a different editor. The
second property simply returns an object, which is an instance of what
is being edited. In this case, the WebPart is being edited so you simply
return this.
In the CreateEditorParts method, note that you are
adding an instance of a new class called as the OPMLEditor. This is the
actual editor which inherits from the EditorPart abstract base class.
Therefore, add a new class in your project and call it OPMLEditor.cs.
The code for OPMLeditor.cs can be seen in Listing 9.
Example 9. OPMLEditor Class
public class OPMLEditor : EditorPart
{
private TextBox opmlCSV;
public OPMLEditor()
{
this.ID = "MyEditorPart";
}
protected override void CreateChildControls()
{
opmlCSV = new TextBox()
{
TextMode = TextBoxMode.MultiLine,
Width = new Unit("300px"),
Height = new Unit("100px")
};
Controls.Add(opmlCSV);
}
public override bool ApplyChanges()
{
EnsureChildControls();
OPMLWebPart part = WebPartToEdit as OPMLWebPart;
if (part != null)
{
List<String> feeds = new List<String>();
string[] rssfeeds = opmlCSV.Text.Split(',');
foreach (string rssfeed in rssfeeds)
{
feeds.Add(rssfeed.Trim());
}
part.FeedURLS = feeds;
}
else
{
return false;
}
return true;
}
public override void SyncChanges()
{
EnsureChildControls();
OPMLWebPart part = WebPartToEdit as OPMLWebPart;
if (part != null)
{
StringBuilder sb = new StringBuilder();
foreach (String rssFeed in part.FeedURLS)
{
sb.Append(rssFeed);
sb.Append(",");
}
if (part.FeedURLS.Count != 0)
sb.Remove(sb.Length - 1, 1);
opmlCSV.Text = sb.ToString();
}
}
}
|
As you can see, the EditorPart abstract base class requires you to implement two methods.
The ApplyChanges method takes the data entered on the
EditorPart and applies it to the WebPart. The SyncChanges method goes
in the reverse direction. When the WebPart is put in edit mode, the
SyncChanges method reads the data from the WebPart, and applies it to
the editing user interface. The editing user interface can be seen in
the CreateChildControls method of the OPML editor.
At this point, you can also edit the feature receiver
to add an instance of the OPMLWebPart to the WebPartPage.aspx that you
had created earlier. Since the steps for that are pretty much the same
as what I've already shown for RSSWebPart, I'm going to skip describing
them. If you prefer, you can also configure the WebPart at runtime right
through the browser.
With the code finished, go ahead and build then
deploy the project. Visit the site in your browser and drop an instance
of the OPMLWebPart in a WebPartZone. Edit the WebPart and you should you
be able to see a multiline text box that would allow you to enter
multiple RSS Feed URLs as comma seperated values. This can be seen in
the Figure 12.
So far so good since the page is coming along well.
You now have two WebParts, one that shows the RSS feed and another that
lets you host a number of RSS feed URLs and allows you to pick one of
them. Wouldn't it be nice if you picked the RSS feed URL from the OPML
WebPart and that feed was rendered in the RSSWebPart on the right?
What you will do next is to make the necessary code changes so these two WebParts can talk with each other.