.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Sharepoint 2010 Connectable webparts

Posted By:Vinodh       Posted Date: September 20, 2011    Points: 40    Category: SharePoint    URL: http://www.dotnetspark.com  

This post describes how to create connectable webparts in Sharepoint 2010
 


 

A webpart that provides a string and another webpart that consumes and displays the value of the string.

These are the tasks to be followed :


1. Creating an interface to define the string that is being provided.

2. Creating a webpart from Visual Studio Webpart item template

3. Creating a provider webpart that defines a string

4. Creating a consumer webpart that uses and displays a string

5. Creating a webpart page and connecting the provider and consumer webpart.



A)  Creating an empty sharepoint project :


  1. Start Visual Studio 2010 in administrator mode.
  2. On the File menu, point to New, and then click Project. If Visual Studio is set to use Visual Basic development settings, on the File menu, click New Project.
  3. In the New Project dialog box, expand the SharePoint node under the language that you want to use, and then select the 2010 node.
  4. In the Templates pane, select Empty SharePoint Project.
  5. For the name of the project, type ConnectableWebParts, and then click OK.
  6. In the What local site do you want to use for debugging? drop-down list, select the local site that you want to use for debugging.
  7. Select Deploy as a farm solution, and then click Finish

 B) Creating an Interface : (what is an Interface?)

   

          public interface ITextBoxString

          {

             string TextBoxString { get; set;}

          }  



 C) Creating a Provider Webpart

  1. In Solution Explorer, click the ConnectableWebParts project.
  2. On the Project menu, click Add New Item.
  3. In the Installed Templates pane, expand the SharePoint node, and then click 2010.
  4. Click Web Part, type StringProvider for the name of the Web Part, and then click Add.
  5. In the StringProvider.cs or StringProvider.vb code file, add the following code to indicate that the StringProvider class implements the ITextBoxString interface.

  public class StringProvider :Webpart, ITextBoxString

  

   6. Add the following code to create a text box, string and a button

 

     private TextBox myTextBox;

     private String _textBoxString = String.Empty;

     private Button myButton;


   7.  Implement the get and set method of the ItextBoxstring interface by adding the following code:

  


 

  [Personalizable()]

public string TextBoxString

{ get

    {

        return _textBoxString;

    }

    set

    {

        _textBoxString = value;

    }

}


                                               (what is personlisation)


  8. CreateChildControls(): instantiates controls and adds new control to the webpart

   

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);

}


9. Code to handel Click event


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);

}

10. Create a method to return tothe ITextBoxString object to share to other webpart.  Apply the ConnectionProviderAttribute attribute to identify the callback method that acts as the provider in a Web Parts connection.



[ConnectionProvider("Provider for String From TextBox", "TextBoxStringProvider")]

public ITextBoxString TextBoxStringProvider()

{

    return this;

}



 D) Creating a Consumer Webpart

  1. In Solution Explorer, click the ConnectableWebParts project.
  2. On the Project menu, click Add New Item.
  3. In the Installed Templates pane, expand the SharePoint node, and then click 2010.
  4. Click Web Part, type StringConsumer for the name of the interface, and then click Add.
  5. In the StringConsumer.cs or StringConsumer.vb code file, add the following code to create objects for an ITextBoxString provider and a text box.

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", "StringConsumer")]
public void TextBoxStringConsumer(ITextBoxString Provider)
{
    _myProvider = Provider;
} 


 E) Connecting the Provider and Consumer Webpart



  1. In Solution Explorer, click the ConnectableWebParts project.
  2. On the Project menu, click Add New Item.
  3. In the Installed Templates pane, click Code.
  4. Click Interface.
  5. Type ITextBoxString for the name of the interface, and then click Add.
  6. In the ITextBoxString.cs or ITextBoxString.vb code file, mark or verify that the ITextBoxString class is public

                                                                                                                                          



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend