Skip to main content

Creating connected web part in SharePoint 2010

Steps:
1. Create interface.
2. Create provider web part by implementing the interface created.
3. Create consumer web part.


Code blocks below:-




----------------------------------------------------------------------
1. Interface ITextBoxString .cs:-

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

namespace ConnectedWebPartSolution
{
    public interface ITextBoxString
    {
        string TextBoxString { get; set; }

    }

}
--------- -------------------------------------------------------------
2. Provider web part StringProvider.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 ConnectedWebPartSolution.Webparts.StringProvider
{
  
    public class StringProvider : System.Web.UI.WebControls.WebParts.WebPart, ITextBoxString
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/ConnectedWebPartSolution.Webparts/StringProvider/StringProviderUserControl.ascx";
        private TextBox myTextBox;
        private String _textBoxString = String.Empty;
        private Button myButton;

        [Personalizable()]
        public string TextBoxString
        {
            get
            {
                return _textBoxString;
            }
            set
            {
                _textBoxString = value;
            }
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            myTextBox = new TextBox();
            Controls.Add(myTextBox);

            myButton = new Button();
            myButton.Text = "Change Text";
            Controls.Add(myButton);
            myButton.Click += new EventHandler(myButton_Click);
        }
        void myButton_Click(object sender, EventArgs e)
        {
            if (myTextBox.Text != String.Empty)
            {
                TextBoxString = myTextBox.Text;
                myTextBox.Text = String.Empty;
            }
        }

        [ConnectionProvider("Provider for String From TextBox")]
        public ITextBoxString TextBoxStringProvider()
        {
            return this;
        }



    }
}

-----------------------------------------------------------------------

3. Consumer web part StringConsumer.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 ConnectedWebPartSolution.Webparts.StringConsumer
{

    public class StringConsumer : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/ConnectedWebPartSolution.Webparts/StringConsumer/StringConsumerUserControl.ascx";
        private ITextBoxString _myProvider;
        private Label myLabel;

        protected override void OnPreRender(EventArgs e)
        {
            EnsureChildControls();
            if (_myProvider != null)
            {
                myLabel.Text = _myProvider.TextBoxString;
            }
        }


        protected override void CreateChildControls()
        {
            Controls.Clear();
            myLabel = new Label();
            myLabel.Text = "Default text";
            Controls.Add(myLabel);
        }

        [ConnectionConsumer("String Consumer")]
        public void TextBoxStringConsumer(ITextBoxString Provider)
        {
            _myProvider = Provider;
        }

    }
}






Comments

Popular posts from this blog

Query SharePoint List - SPQuery vs SPSiteDataQuery vs CrossListQueryInfo

There are three different classes you can use to query SharePoint list:-
1.SPQuery
Used for query a data from a specific list and also used for join two lists.
Use full Links:-
·http://sharepointfordeveloper.blogspot.in/2011/10/step-by-step-spquery-list-joins.html#!/2011/10/step-by-step-spquery-list-joins.html
·http://mysharepointwork.blogspot.in/2010/04/spquery-examples.html

2.SPSiteDataQuery