IT tutorials
 
Applications Server
 

SharePoint 2010 : Writing a WebPart (part 4) - WebPart Communication

5/8/2013 7:37:59 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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.

Figure 13. Connecting WebParts through the browser.

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.

 
Others
 
- SharePoint 2010 : Writing a WebPart (part 3) - Writing the OPML WebPart and a WebPart Editor
- SharePoint 2010 : Writing a WebPart (part 2) - Configuring the WebPart During Deployment
- SharePoint 2010 : Writing a WebPart (part 1) - Writing the RSSFeed WebPart
- Microsoft Dynamics CRM 2011 : Reporting with Excel (part 4) - Uploading Excel Reports to the Reports List in Microsoft Dynamics CRM
- Microsoft Dynamics CRM 2011 : Reporting with Excel (part 3) - Exporting Dynamic Data to Excel PivotTables
- Microsoft Dynamics CRM 2011 : Reporting with Excel (part 2) - Exporting Dynamic Data to Excel Worksheets
- Microsoft Dynamics CRM 2011 : Reporting with Excel (part 1) - Exporting Static Data to Excel Worksheets
- Active Directory 2008 : Automating the Creation of User Accounts (part 2) - Importing Users with LDIFDE
- Active Directory 2008 : Automating the Creation of User Accounts (part 1)
- SharePoint 2010 : SharePoint Pages - Deploying Pages
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us