Click here to Skip to main content
15,889,096 members
Articles / Web Development / ASP.NET

Connecting Custom Web Parts in SharePoint

Rate me:
Please Sign up or sign in to vote.
4.63/5 (19 votes)
10 Jun 2009CPOL4 min read 161.8K   1.3K   24  
Describes how to connect two SharePoint custom Web Parts using the Visual Studio Extension for Windows SharePoint Services.
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using CommunicationInterface;

namespace ConsumerWebPart
{
    [Guid("ab395c89-1146-4964-9528-f42153bf5c06")]
    public class ConsumerWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        Label lblTitle;
        Label lblResult;

        ///// the string info consumer from custom reciever   //
        ICommunicationInterface connectionInterface = null;
        // The consumer webpart  must define a method that would accept the interface as an parameter and must be decorated with ConnectionConsumer attribute      
        [ConnectionConsumer("Parameter1 Consumer", "Parameter1 Consumer")]
        public void GetConnectionInterface(ICommunicationInterface _connectionInterface)
        {
            connectionInterface = _connectionInterface;
        }
        /////////////////////////////////////////////////////////  

        public ConsumerWebPart()
        {
            this.Title = "Consumer WebPart";
            this.ExportMode = WebPartExportMode.All;
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Table tbl;
            TableRow row;
            TableCell cell;

            // A table for layout 
            tbl = new Table();
            row = new TableRow();
            cell = new TableCell();

            // first row for title
            cell.VerticalAlign = VerticalAlign.Middle;
            cell.HorizontalAlign = HorizontalAlign.Center;
            lblTitle = new Label();
            lblTitle.Text = "This WebPart will recieve a parameter from a provider:";
            cell.Controls.Add(lblTitle);
            row.Controls.Add(cell);
            tbl.Controls.Add(row);

            //second row for result
            row = new TableRow();
            cell = new TableCell();
            cell.VerticalAlign = VerticalAlign.Middle;
            cell.HorizontalAlign = HorizontalAlign.Center;
            lblResult = new Label();
            //check the connectionInterface for recieving the parameter1
            if (connectionInterface != null)
            {
                lblResult.Text = connectionInterface.Parameter1+" is recieved!";
            }
            else
            {
                lblResult.Text = "nothing is recieved!";
            }
            cell.Controls.Add(lblResult);
            row.Controls.Add(cell);
            tbl.Controls.Add(row);

            //add table to webpart
            this.Controls.Add(tbl);           
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Business Analyst AIRIC
Iran (Islamic Republic of) Iran (Islamic Republic of)
MCP (Microsoft Certified Professional)
Senior Business Analyst in AIRIC (Automotive Design and Research Company).
Capabilities and interests:
System Analysis and design, .NET Framework, ASP.NET, Windows Application, Windows Workflow Foundation, SharePoint Customization and Development,SQL Server, BPMN and UML.
Master of Industrial Engineering from Poly-Technic of Tehran

Comments and Discussions