3. WebPart Communication
Sometimes different WebParts on the same page have a
need to talk to each other. Events in one WebPart need to reflect
changes in another WebPart. Enabling this process is commonly referred
to as WebPart communication.
WebPart communication requires you to do the following four things:
Decide on what needs to be communicated. This becomes your communication contract, usually implemented as an interface.
Create a provider WebPart that returns an instance of the interface that represents the contract.
Create one or more consumer WebParts that will get an instance of the interface and suitably act upon it.
Connect those consumers and the provider.
So, let's begin. In the RSS WebParts, what you intend
to communicate is an RSS URL. Therefore, your communication contract
looks like the following code:
public interface IRSSFeedContract
{
String RSSUrl { get; set; }
}
Go ahead and add the preceding code as an interface in your project.
Next, you need to decide on who the provider is. In
this case since the RSSWebPart holds an instance to the RSS URL in
question, that seems to be the suitable candidate to be a provider. In
RSSWebPart.cs, make the public class RSSWebPart implement the
IRSSFeedContract interface. This will require you to make some changes
to the RSSWebPart class. The final code for the RSSWebPart class can be
seen in Listing 13.
Example 13. RSSWebPart Class with the Communication Code
[ToolboxItemAttribute(false)]
public class RSSWebPart : WebPart, IRSSFeedContract
{
[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);
}
[ConnectionProvider("Rss service Provider")]
public IRSSFeedContract GetRssCommunicationPoint()
{
return this as IRSSFeedContract;
}
}
|
If you note closely, the RSSUrl Property of the
RSSWebPart acts as the implementation for the IRSSFeedContract. Also,
you will notice a method decorated with the ConnectionProvider
attribute. This method is called by the SharePoint framework when it
needs to get a reference to the provider. This reference to the provider
in turn is provided to all the consumers on the page. Since RSSWebPart
implements IRSSFeedContract, you simply return this.
Next, let's make changes to the consumer. The consumer in this case is the OPMLWebPart. Go ahead and add the code is shown in Listing 14 to the OPMLWebPart.
Example 14. OPMLWebPart's Consumer Code
private IRSSFeedContract theProvider;
[ConnectionConsumer("Rss service Consumer")]
public void InitializeProvider(IRSSFeedContract provider)
{
theProvider = provider;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (theProvider != null)
{
theProvider.RSSUrl = rbl.SelectedValue;
}
}
|
As you can see from Listing 4-15,
the InitializeProvider method is called by the framework after it has a
reference to the provider (which it got by calling the
GetRssCommunicationPoint method in Listing 4-14).
The instance of IRSSFeedContract is passed as a parameter to
InitializeProvider. By using this parameter in the OnPreRender method,
your WebPart now has the opportunity of setting the appropriate value of
the RSSUrl. Setting a value on RSSUrl thereby renders the proper RSS
Feed in the provider.
Go ahead and build then deploy the WebPart. In your
browser, go to any suitable page with two WebPartZones in it. Drop an
instance of the OPMLWebPart and the RSSWebPart in each of the zones.
Then like before, edit the OPMLWebPart and provide a few RSS Feed URLs.
Next connect the two WebParts, as shown in Figure 13.
You should see this running in action. You
will note that choosing an alternate feed from the OPMLWebPart changes
the RSSFeed displayed in the RSSWebPart. The WebParts are now talking
with each other.