/* Copyright (c) 2004-2005 BluedogLimited.com Author: Maurice J. Prather Project: Scratch Description: Sample code designed to illustrate how to create a WebPart that operates as both an ICellConsumer and ICellProvider. $Date: 2005/03/22 00:24:24 $ */ using System; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.SharePoint.WebPartPages; using Microsoft.SharePoint.WebPartPages.Communication; namespace BluedogLimited.WebParts { /// /// Class - A test bed /// public class Scratch : WebPart, ICellConsumer, ICellProvider { private bool connectionEstablished = false; private bool connectionRequested = false; private bool partIsConsumer = true; private string message = null; private Button btnFireConnection = null; public event CellConsumerInitEventHandler CellConsumerInit; public event CellProviderInitEventHandler CellProviderInit; public event CellReadyEventHandler CellReady; void ICellConsumer.CellProviderInit (System.Object sender, CellProviderInitEventArgs e) { this.Controls.Add (TextPanel ("Field display name (pi): " + e.FieldDisplayName)); this.Controls.Add (TextPanel ("Field name (pi): " + e.FieldName)); } void ICellConsumer.CellReady (System.Object sender, CellReadyEventArgs e) { this.message = Convert.ToString (e.Cell); } void ICellProvider.CellConsumerInit (System.Object sender, CellConsumerInitEventArgs e) { this.Controls.Add (TextPanel ("Field display name: " + e.FieldDisplayName)); this.Controls.Add (TextPanel ("Field name: " + e.FieldName)); } #region Constructors public Scratch () { this.Init += new EventHandler (InitEventHandler); this.PreRender += new EventHandler (PreRenderEventHandler); } // End of Scratch constructor #endregion #region Private static methods private static string RecordExceptionInformation (Exception ex) { return "
Exception type:" + System.Web.HttpUtility.HtmlEncode (ex.GetType().FullName) + "
" + "
" + System.Web.HttpUtility.HtmlEncode (ex.Message) + "
" + "
" + System.Web.HttpUtility.HtmlEncode (ex.StackTrace) + "
"; } // End of RecordExceptionInformation private static Panel TextPanel (string text) { Panel panel = new Panel (); Label label = new Label (); label.Text = System.Web.HttpUtility.HtmlEncode (text); panel.Controls.Add (label); return panel; } // End of TextPanel #endregion #region Public connection-related methods /// /// Method - Required override to establish proper communication chain /// public override void EnsureInterfaces() { try { RegisterInterface ("Cell Provider", InterfaceTypes.ICellProvider, UnlimitedConnections, ConnectionRunAt.Server, this, "cellprovider_WPQ_", "Provide data to...", "Provides a random number", true); RegisterInterface ("Cell Consumer", InterfaceTypes.ICellConsumer, UnlimitedConnections, ConnectionRunAt.Server, this, "cellconsumer_WPQ_", "Receive data from...", "Receive cell data from a provider", true); } catch (System.Security.SecurityException) { this.message = "Unable to register interfaces due to code access security restrictions"; } } /// /// Method - Required override to establish proper communication chain /// public override ConnectionRunAt CanRunAt () { return ConnectionRunAt.Server; } // End of ConnectionRunAt /// /// Method - Required override to establish proper communication chain /// public override void PartCommunicationConnect (string interfaceName, WebPart connectedPart, string connectedInterfaceName, ConnectionRunAt runAt) { // Set the flag indicating a connection has been established... this.connectionEstablished = true; // Check to see if the current registering interface is known... if (interfaceName == "Cell Provider") { this.partIsConsumer = false; } } // End of PartCommunicationConnect /// /// Method - Optional override /// public override void PartCommunicationInit () { // // Note: In this example, the contents of this method are // unnecessary (only used for display purposes). // // Check to see if a handler has been attached to the CellProvider event... if (CellProviderInit != null) { // Create and initialize the args object... CellProviderInitEventArgs cellProviderInitEventArgs = new CellProviderInitEventArgs(); cellProviderInitEventArgs.FieldDisplayName = "Random number"; cellProviderInitEventArgs.FieldName = "RandomNumber"; // Fire the CellProviderInit event... CellProviderInit (this, cellProviderInitEventArgs); } // Check to see if a handler has been attached to the CellConsumer event... if (CellConsumerInit != null) { // Create and initialize the args object... CellConsumerInitEventArgs cellConsumerInitEventArgs = new CellConsumerInitEventArgs(); cellConsumerInitEventArgs.FieldDisplayName = "Numero"; cellConsumerInitEventArgs.FieldName = "NumeroAlAzar"; // Fire the CellProviderInit event... CellConsumerInit (this, cellConsumerInitEventArgs); } } // End of PartCommunicationInit /// /// Method - Required override, communication work horse /// public override void PartCommunicationMain () { // Check to see if a handler has been attached to the CellReady event... if (CellReady != null && this.connectionRequested) { // Create and initialize the args object... CellReadyEventArgs cellReadyEventArgs = new CellReadyEventArgs(); cellReadyEventArgs.Cell = new Random().Next(); // Fire the CellReady event... CellReady (this, cellReadyEventArgs); } } // End of PartCommunicationMain /// /// Method - Optional override /// public override InitEventArgs GetInitEventArgs (string interfaceName) { // Create and intialized the args object... CellConsumerInitEventArgs cellConsumerInitEventArgs = new CellConsumerInitEventArgs(); cellConsumerInitEventArgs.FieldDisplayName = "Random number"; cellConsumerInitEventArgs.FieldName = "MyRandomNumber"; // Return the args object... return (cellConsumerInitEventArgs); } // End of GetInitArgs #endregion #region Protected methods protected override void RenderWebPart (HtmlTextWriter output) { try { // Display message, if the webpart is a consumer... if (this.partIsConsumer) { this.Controls.Add (TextPanel ("Message: " + message)); } this.RenderChildren (output); } catch (Exception ex) { this.message += Scratch.RecordExceptionInformation (ex); output.Write (this.message); } } // End of RenderWebPart #endregion private void btnFireConnection_Click (object sender, EventArgs e) { this.connectionRequested = true; } // End of btnFireConnection_Click private void InitEventHandler (object sender, EventArgs e) { btnFireConnection = new Button (); btnFireConnection.Text = "Fire the connection!"; btnFireConnection.Click += new EventHandler(btnFireConnection_Click); this.Controls.Add (btnFireConnection); } // End of InitEventHandler private void PreRenderEventHandler (object sender, EventArgs e) { // Display simple text indicating no connection has been established... if (!this.connectionEstablished) { this.Controls.Add (TextPanel("This WebPart is currently not connected to any other WebPart")); } // Since this web part is a consumer, remove the button from // the collection and set it to null... if (this.partIsConsumer) { this.Controls.Remove (this.btnFireConnection); this.btnFireConnection = null; } } // End of PreRenderEventHandler } // End of Scratch class } // End of the BluedogLimited.WebParts namespace