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 :
- Start Visual Studio 2010 in administrator mode.
- 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.
- In the New Project dialog box, expand the SharePoint node under the language that you want to use, and then select the 2010 node.
- In the Templates pane, select Empty SharePoint Project.
- For the name of the project, type ConnectableWebParts, and then click OK.
- 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.
- 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
- In Solution Explorer, click the ConnectableWebParts project.
- On the Project menu, click Add New Item.
- In the Installed Templates pane, expand the SharePoint node, and then click 2010.
- Click Web Part, type StringProvider for the name of the Web Part, and then click Add.
- In the StringProvider.cs or StringProvider.vb code file, add the following code to indicate that the StringProvider class implements the ITextBoxString interface.
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
- In Solution Explorer, click the ConnectableWebParts project.
- On the Project menu, click Add New Item.
- In the Installed Templates pane, expand the SharePoint node, and then click 2010.
- Click Web Part, type StringConsumer for the name of the interface, and then click Add.
- 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;
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; }
- In Solution Explorer, click the ConnectableWebParts project.
- On the Project menu, click Add New Item.
- In the Installed Templates pane, click Code.
- Click Interface.
- Type ITextBoxString for the name of the interface, and then click Add.
- In the ITextBoxString.cs or ITextBoxString.vb code file, mark or verify that the ITextBoxString class is public
read more
No comments:
Post a Comment