Sunday, 13 May 2012

Creating Connectable Web Parts in SharePoint Foundation

Step1) Create Sharepoint Project using Visual Studio 2010
              1) select .net framework 3.5
              2)select empty project

Step2)  Add 2 webparts
                 1.Consumer WebPart
                 2.Provider Webpart
Step3)  add common interface inorder to share information between webparts(i.e consumer & provider)

Step 4) Deploy the Project

Step5) create new "WebPart" page.  wikipage doesn't provide connecting weparts.


Example
         
Provider webpart code.


filename:
ProviderWp.cs





using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;


namespace connwp_demo.ProviderWp
{
    [ToolboxItemAttribute(false)]
    public class ProviderWp : WebPart,IProvConsBridge
    {
        TextBox box = new TextBox();
        String _text = string.Empty;
        Button btn = new Button();
        protected override void CreateChildControls()
        {
            box.Text = "This info will be passed to Consumer";
            this.Controls.Add(box);
            btn.Text = "Send Data";
            this.Controls.Add(new System.Web.UI.LiteralControl("<br/>"));
            btn.Click += new EventHandler(btn_Click);
            this.Controls.Add(btn);
        }


        void btn_Click(object sender, EventArgs e)
        {
            _text = box.Text;
            box.Text=string.Empty;
        }


        public string Message
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
            }
        }
        [ConnectionProvider("Conn provider")]
        public IProvConsBridge ProviderInfo()
        {
            //this.Message = box.Text;
            return this;
        }
    }
}

Consumer Webpart Code

filename:ConsumerWp.cs
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace connwp_demo.ConsumerWp
{
    [ToolboxItemAttribute(false)]
    public class ConsumerWp : WebPart
    {
        IProvConsBridge bridge;
        Label lbl = new Label();
        protected override void CreateChildControls()
        {
            lbl.Text = "info updated by provider";
            this.Controls.Add(lbl);
        }

        protected override void OnPreRender(EventArgs e)
        {
            EnsureChildControls();
            if (bridge != null)
            {
                lbl.Text = bridge.Message;
            }
        }

        [ConnectionConsumer("consumer")]
        public  void GetProvider(IProvConsBridge fromp)
        {
            bridge = fromp;
        }
    }
}


common interface code (it must be an interface, because .NET doesn't support multiple inheritance).  interface must be public

filename:IProvConsBridge.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace connwp_demo
{
    public interface IProvConsBridge
    {
        String Message{get;set;}
    }
}


Deploy the Solution


Create "WebPart" page ,  wikipage don't not support connecting webparts, but supports individual webparts.


add webparts to a page.  1) ProviderWp  2) consumer Webpart

OUTPUT:





No comments:

Post a Comment