Table of ContentsPage 1 - Introduction
- Coding Example
- Step 1 – Create the ICellConsumer Interface Class
- Step 2 – Declare the ICellConsumer Event
- Step 3 – Override the EnsureInterfaces Method
- Step 4 – Override the CanRunAt Method
- Step 5 – Override the PartCommunicationConnect Method
Page 2 - Step 6 – Override the PartCommunicationInit Method
- Step 7 – Override the GetInitEventArgs Method
- Step 8 – Override the CellProviderInit Event Handler
- Step 9 - Override the CellReady Event
- Step 10 – Override the RenderWebPart Method
- Step 11 – Override the CreateChildControls Method
- About the Author
- Download Sample Code
Step 6 – Override the PartCommunicationInit Method This method is called when the Web Part needs to fire any initialization events. The Web Part will fire any event end with "Init" . For example CellConsumerInit. The Init parameters are used when a Web Part needs to provide information about itself to other Web Parts. The developer has the option whether to override this method or not. ///<summary> ///PartCommunicationInit ///----At this time, a part should fire any
events that end with 'Init' ///----if they can. ///----## ICellConsumer Example: fire "CellConsumerInit" ///</summary> publicoverridevoidPartCommunicationInit() { //If the connection wasn't actually formed then
don't want to send Init event if(_cellConnectedCount > 0) { //If there is a listener, send init event if (CellConsumerInit != null) { //Need to create the args for the CellConsumerInit event CellConsumerInitEventArgscellConsumerInitArgs = new CellConsumerInitEventArgs(); //Set the FieldName cellConsumerInitArgs.FieldName = cellName; //Fire the CellConsumerInit event. //This basically tells the Provider Web Part what type of //cell the Consumer is expecting in the CellReady event. CellConsumerInit(this, cellConsumerInitArgs); } } } Figure 8 - PartCommunicationInit method. Step 7 – Override the GetInitEventArgs Method This method is used by SharePoint architecture to collect the necessary information it needs to build the transformer dialog. A transformer dialog box will come into action when ICellConsumer interface connect with IRowProvider interface. This method should only be implemented with interfaces that participate in transformer dialog box. publicoverrideInitEventArgsGetInitEventArgs(stringinterfaceName) { //Check if this is my particular cell interface if (interfaceName ==
"Ex1CellConsumerInterface_WPQ_") { EnsureChildControls(); //Need to create the args for the CellConsumerInit event CellConsumerInitEventArgscellConsumerInitArgs = new CellConsumerInitEventArgs(); //Set the FieldName cellConsumerInitArgs.FieldName = cellName; //return the InitArgs return(cellConsumerInitArgs); } else { return(null); } } Figure 9 - GetInitEventArgs method Step 8 – Override the CellProviderInit Event Handler The connected provider Web Part will use this method to pass initialization information to the consumer Web Part. ///<summary> /// The CellProviderInit event handler. /// The Provider part will fire this event
during its /// PartCommunicationInit phase ///</summary> ///<param name="sender">Provider Web
Part</param> ///<param name="cellProviderInitArgs">Theargs passed
by the Provider /// </param> publicvoidCellProviderInit(object sender, CellProviderInitEventArgscellProviderInitArgs) { //This is where
the Consumer part could see what type of "Cell" //the Provider will
be sending. connectedFieldName = SPEncode.HtmlEncode(cellProviderInitArgs.FieldDisplayName); } Figure 10 - CellProviderInit event handler The above code extract the field name of the provider Web Part connected. Step 9 - Override the CellReady Event The connected provider Web Parts will call this method during its PartCommunicationMain to pass its primary data to the consumer Web Part. publicvoidCellReady(object sender, CellReadyEventArgscellReadyArgs) { //Set the label text to the value of the
"Cell" that was passed by the Provider if(cellReadyArgs.Cell != null) { displayLabel.Text =cellReadyArgs.Cell.ToString(); } } Figure 11 - CellReady event The label control in consumer Web Part receives a value from provider Web Part in above code. Step 10 – Override the RenderWebPart Method RenderWebPart method is overridden by webpart base class. This method is called by the Web Part architecture to render the HTML for the body of the Web Part. ///<summary> /// Render this Web Part control to the
output parameter specified. ///</summary> ///<param name="output"> The HTML writer
to write out to </param> protectedoverridevoidRenderWebPart(HtmlTextWriter output) { //Make sure all child controls are created EnsureChildControls(); if(interfaceRegistrationError) { //There is no interfaces connected. output.Write("
<FONT COLOR='RED'>An error occured while trying to register the interface.</FONT>"); } else { //if cell connected if(_cellConnectedCount > 0) { //Create a header output.RenderBeginTag(HtmlTextWriterTag.H4); //Write the header test output.Write("Web
Part is connected on Server Side"); output.Write(connectedWebpartName); output.Write("
Web Part"); //Create a break output.RenderBeginTag(HtmlTextWriterTag.Br); output.Write("The
field "); output.Write(connectedFieldName); output.Write("
is providing the information"); output.RenderEndTag(); //Create a break output.RenderBeginTag(HtmlTextWriterTag.Br); output.RenderEndTag(); //Render the button displayLabel.RenderControl(output); } else { //There is no interfaces connected. output.Write("
<FONT COLOR='RED'>No Cell Providers Connected. Please select a Cell Provider to display value in the Label.</FONT>"); } } } Figure 12 - RenderWebPart Method The code is looking for any errors that occur during the interface registration before rendering any controls. An error message will display if an error has occurred. Otherwise, if the Web Part is connected, the controls will be rendered with the information of the title of the connected Web Part and filed its consuming the data. Step 11 – Override the CreateChildControls Method This method creates all the user interface controls necessary for the Web Part. In this example, the Label control: ///<summary> ///Creates all
the user interface controls necessary for the web part ///</summary> protectedoverridevoidCreateChildControls() { displayLabel = new Label(); Controls.Add(displayLabel); } Figure 13 - CreateChildControls Method Uploading the ICellConsumer Web Part Navigate to SharePoint web page top right hand corner and select Modify My Page or Modify Shared Page depending on whether the end user in Personal or Shared view. Select Add Web Part | Import option. Browse to the "CellConsumerWebPart.dwp" file and click Upload button. This will upload the Cell Consumer Web Part as displayed below:  Figure 14 - Upload the Cell Consumer Web Part.
Drag and drop the "Cell Consumer Web Part" to the page as displayed below.  Figure 15 - Cell Consumer Web Part
The above Web Part indicates it is not connected to a Provider Web Part. The end user has the option to connect this consumer
Web Part with a compatible provider Web Part any time. (Please refer to the article
"Connectable Web Parts in SharePoint Portal 2003"
for more information regarding how to connect a Web Part.) The following figure displays the Cell Consumer Web Part connected to a Cell Provider Web Part.  Figure 16 - The Cell Consumer Web Part is connected to a Cell Provider Web Part
It displays the title of the connected Web Part in the header section. In this case, it is connected to Cell Provider Web Part. The Consumer Web Part also display the information about the field that is connected in the provider Web Part. For more information regarding ICellProvider Web Parts, Please visit the article
"Developing Web Parts with ICellProvider Interface". The above Cell Consumer code sample is a simple exercise to demonstrate the capability of a Cell Consumer Web Part. The developers can create more complex connectable Web Parts to suit their needs. About the Author Gayan Peiris is a Senior Consultant for Unique World Pty Ltd (www.uniqueworld.net) in Canberra, Australia. He is a MCSD with MCAD, MCP, Bcom and MIT certifications. Gayan has designed and developed Microsoft Web and Windows solutions since 1999. His expertise lies in developing scalable, high-performance applications with Microsoft Enterprise Server Products in .NET technology .His core skills are ADO.NET, ASP.NET, C#, VB.NET, Web Services, XML, SharePoint Portals, DNA and SQL Server. << Introduction |