Creating a Hello World Connectable Web Parts in SharePoint.

Posted: February 14, 2009 in SharePoint
Tags:

To write connectable web parts we need to do the following

First we need to define our own interface that will specify the data we want to pass from one web part to another.

The provider web part needs to do the following

  • Implement the interface.
  • Create a property which would be returning a reference to the interface.
  • The property should be decorated with ConnectionProvider attribute.

The consumer web part needs to do the following

  • It should contain the method which would receive the interface.
  • The method should be decorated with ConnectionConsumer attribute.

Keeping the above information in mind let’s start

 Create a new web part project within Visual Studio 2008. (HelloWorldConnectedWebPart)

Right click on the Project

Select Add New ItemàSharePointàWebPart.

Now rename the webpart1.cs and webpart2.cs class as  HWProviderWebPart and HWConsumerWebPart respectively.

Now right click the project and add a new interface class.

    public interface IStringData

    {

        string ProviderStringInfo { get; }

    }

Our Provider class should implement this interface and define one property which would be returning the reference to the interface.

[Guid("56978c39-1958-4128-a979-b9feaf2feb46")]

    public class HWProviderWebPart : WebPart, IStringData

    {

        public HWProviderWebPart()

        {

        }

        protected override void CreateChildControls()

        {                    

        }

        // the string info that would be passed to the consumer

        protected string myInfo = “Hello World”;    

        // implement the property defined in the interface

        public string ProviderStringInfo

        {

            get { return myInfo; }

        }     

        // create a property which would be returning the interface reference

        // decorate it with ConnectionProvider

        [ConnectionProvider("String Provider")]

        public IStringData ConnectionInterface()

        {

            return this;

        }

    }

 

Now let’s move to our Consumer Web Part.

[Guid("3575c6de-e21a-4e5a-b7f0-fe1aa4844402")]

    public class HWConsumerWebPart : System.Web.UI.WebControls.WebParts.WebPart

    {

        public HWConsumerWebPart(){

        }

        protected override void CreateChildControls(){      

        }

 

        IStringData myProviderInterface = null;

        // The Consumer class should define a method that would accept

        // the interface as an parameter

        // Should be decorated with ConnectionConsumer attribute

 [ConnectionConsumer("String Provider")]

        public void GetInterface(IStringData providerInterface)

        {

            myProviderInterface = providerInterface;

        }

 

        protected override void Render(HtmlTextWriter writer)

        {

            try

            {  

                // priting the value provided by provider web part

                writer.Write(myProviderInterface.ProviderStringInfo);

            }

            catch(Exception ex)

            {

                writer.Write(“Error info “ + ex.Message);

            }

        }   

    }

 

Now build the project. ( Remove errors if any)

Right click the project.

Select Properties – Debug — Start browser with url ( Specify the site where the web part should be deployed)

Right click the project and select Deploy.

After Deploy Succeeds ,

Go to site actions — Site Settings — WebParts( Inside galleries) –Click on New– Select both the Provider and consumer web part — Populate Gallery.

Go to your home page —  Edit page — Add both the web parts –Select the provider web part — Connections and Specify the connection.

That’s it..

Comments
  1. Rishi jagati says:

    hi, can you suggest me how to connect two webParts programmatically?

  2. vinodh says:

    I get this error: Error info Object reference not set to an instance of an object

  3. swapna says:

    for me it is not working. i am getting error messgae
    Error 1 Inconsistent accessibility: parameter type ‘Connwebpartsample.IStringData’ is less accessible than method ‘Connwebpartsample.Consumerhelloworldwebpart.Consumerhelloworldwebpart.GetInterface(Connwebpartsample.IStringData)’

  4. swapna says:

    i am getting below error message
    Inconsistent accessibility: return type ‘connectablewebpart.IUser’ is less accessible than method ‘connectablewebpart.ProviderWP.cs.ProviderWP.ConnProviderMethod()’.

    please tell me how to resolve. i am working on sharepoint2010.

  5. vijay bhaskar says:

    Getting error

    ‘HelloWorldConnectedWebPart.HWProviderWebPart’ does not implement interface member ‘HelloWorldConnectedWebPart.IStringData.ProviderStringInfo’

  6. vijay bhaskar says:

    ‘HelloWorldConnectedWebPart.HWProviderWebPart’ does not implement interface member ‘HelloWorldConnectedWebPart.IStringData.ProviderStringInfo’

  7. Nidhi says:

    I can deploy the solution but get and an error “Object Refrence not set to an instance of the Object” when i try to add these webparts on the webpage.

  8. cts says:

    It works. :) excellent.

  9. deepak goel says:

    simple and brief , wonderful article to begin with connected webparts.

  10. TR says:

    Nice – but how to consume data (in my custom webpart) from the out-of-the-box dataform web part.

    The kind of connection where you click a cell value (link) and the consumer web part shows items filteret by the recieved value ?

  11. upma says:

    Excellent…..worked like majic…..

  12. liera says:

    I’m having problem on the consumer…

    -when deployed and refresh the page my consumer got this error
    “Error info The method or operation is not implemented”

  13. Gustavo Galan says:

    Please help, I received this error when building

    Error on HWProviderWebPart
    [ConnectionProvider("String Provider")]
    public IStringData ConnectionInterface()
    {
    return this;
    }

    Error 1 Cannot implicitly convert type ‘HelloWorldConnectedWebPart.HWProviderWebPart’ to ‘HelloWorldConnectedWebPart.IStringData’. An explicit conversion exists (are you missing a cast?)

    • Matthew says:

      I had the same issue and didn’t implement the IStringData interface in the HWProviderWebPart class.

      Make sure you implement the IStringData interface with the HWProviderWebPart:
      public class HWProviderWebPart : System.Web.UI.WebControls.WebParts.WebPart, IStringData

    • vijay bhaskar says:

      I think you forgot to inherit the Interface IStringData.. Please make sure that one..

  14. [...] Creating a Hello World Connectable Web Parts in SharePoint [...]

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s