Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

ASP.NET 2.0 Web Part Infrastructure and SharePoint 2007 : Page 3

Find out how to make your Web Parts communicate with each other and share data.


advertisement
Establishing Communication with the RSSRender WebPart
In the previous article you saw a Web Part that rendered an RSS feed, where I specified the URL for this feed as a public editable property to the Web Part. I've reproduced the code for that Web Part for your convenience in Listing 3.

Now you have a Web Part that displays an RSS feed. You also have a Web Part that displays a list of RSS feeds and allows the user to pick one of them. The obvious next question is, how can I make these talk to each other and create a blog reader? In other words, how can I connect these Web Parts together?

Before I dive into code, let me cover some basic theory about Web Part communication first.

ASP.NET 2.0 Web Part communication lets you connect Web Parts so they can share data. To establish such communication, you need to do four things.

  1. Figure out what information needs to be communicated. This information needs to be understood by both ends of the communication. A good way to achieve this is by writing an interface that both sides will understand. This interface is also referred to as the communication contract. If both sides do not speak the same communication contract, you may use transformers to perform the translation.
  2. Write a provider Web Part. This Web Part has the responsibility of providing a service or data, so in our example the RSSRender WebPart provides the service of displaying an RSS feed. Anyone who wants to use this service can become a consumer and ask the RSSRender WebPart to provide the rendering of the appropriate feed. The Provider Web Part needs to contain a method decorated with the ConnectionProvider attribute, which returns the contract that you defined in step 1.
  3. Write or use one or more consumer Web Parts. This consumer Web Part needs to contain a method decorated by the ConnectionConsumer attribute. This method needs to accept a parameter of the same type as the contract you defined in step 1.
  4. Connect the consumers with the provider. You can make this connection statically in the WebPartManager, programmatically, or using the ConnectionsZone.
With the theory explained, I will now begin to modify the code for the two Web Parts I have—the OPMLEditor WebPart and the RSSRender WebPart so they can begin talking with each other.

The first step is to write an interface that conveys the information to be communicated. In this case, the information is simply an RSS feed URL. You can see this interface below:

public interface IRssFeedContract { String RSSUrl {get;set;} }

The second part is to modify the RSSRender WebPart so it becomes a provider Web Part in this communication scenario. To keep things simple, I am going to have the RSSRender WebPart itself implement the IRssFeedContract. This involves creating a new property as shown below:

string IRssFeedContract.RSSUrl { get { return rssUrl; } set { rssUrl = value ; } }

As you can tell from Listing 1, the rssURL private variable contains the feed URL of the RSS feed being rendered.

Also, I need to create a method decorated by the ConnectionProvider attribute which returns an instance of the shared understanding contract (the IRssFeedContract). You can see this below:



[ConnectionProvider("Rss service Provider")] public IRssFeedContract GetRssCommunicationPoint() { return this as IRssFeedContract; }

The third step is to finish up the consumer Web Part. The consumer Web Part will manipulate the provider and set the relevant data on the provider Web Part, so the provider Web Part can display the RSS feed. To do that, the Consumer Web Part must contain an instance of the provider. You set this instance by a method that is called by the Web Part framework. The Web Part framework identifies the method appropriately because it is decorated by the ConnectionConsumer attribute. You can see the implementation below:

private IRssFeedContract theProvider; [ConnectionConsumer("Rss service Consumer")] public void InitializeProvider(IRssFeedContract provider) { theProvider = provider ; }

After the preceding method is called, the consumer Web Part contains an instance of the provider. With this instance available, the consumer can now set the appropriate property on the provider Web Part, as shown below:

protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (theProvider != null) { theProvider.RSSUrl = rbl.SelectedValue ; } }

As you can see, if the connection has indeed been made, the consumer Web Part specifies the RSS URL to the provider Web Part.

The last and final step is to establish the communication between the provider and the consumer.

Go ahead and add the RSSRender/Import WebPart to a WebPartZone on your page. I am going to add it to WebPartZone2 as shown in Figure 4.

 
Figure 4: The two Web Parts added to our simple framework.
 
Figure 5: Setting the WebPartManager in display mode "Connect."
Next, go ahead and switch the WebPartManager's display mode to Connect in the simple framework I wrote in the previous article. You can see this in Figure 5.

When the WebPartManager is in DisplayMode connect, you can see that the Web Parts start showing the "Connect" verb in their menus as shown in Figure 6. Go ahead and click the "Connect" verb for either Web Part. In my case I am going to click Connect for the Consumer WebPart, that is, the OPMLEditor WebPart. When you click Connect, the ConnectionsZone displays a UI similar to the one shown in Figure 7.
 
Figure 6: Web Part with the Connect verbs visible.
 
Figure 7: Another Web Part with the Connect verbs visible.
Next, go ahead and click the "Create a connection to a provider" link. The ConnectionsZone should now ask you for the appropriate provider information. Fill in the values as shown in Figure 8 and click Connect. The ConnectionsZone should display the currently setup connections (see Figure 9).

 
Figure 8: Specifying the appropriate connection in the Connections Zone.
 
Figure 9: The Connections Zone displaying that my Web Parts are now connected.
Now log off, and then select one of the RSSFeeds from the OPMLEditor WebPart.

 
Figure 10: The connected Web Parts in action: you have just created a Web-based blog reader.
As you can see in Figure 10, you have very easily created a simple Web-based blog reader using the ASP.NET 2.0 Web Part framework. Your Web Parts are now successfully communicating.

Creating a blog reader is not the only thing you can do with this framework. In fact, you can create a sophisticated framework that can power almost any application. You could use other ASP.NET features, such as site-map providers and URL rewriting, to create a generic framework to set up any Web application you want. On my Web site (www.winsmarts.com), you can find a simple starter kit called "SpareJoint" that allows you to set up any such site and add custom functionality. It goes without mentioning that this starter kit is offered for no support or expressed or implied warranties—use it at your own risk.

But, so far I haven't even begun talking about SharePoint 2007. Time to switch gears and understand the connection between ASP.NET 2.0 and SharePoint 2007.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap