In this task, you create a SharePoint 2010 Empty project in Microsoft Visual Studio 2010.
Aspired to become a SharePoint? Explore the post to discover the know-how on the " SharePoint Training" course
Figure 1: Select the Empty SharePoint Project type
The ConnectTwoWebParts project is created and Solution Explorer is displayed.
In this task, you create the Web Part connection interface IProject that is responsible for exchanging connection information between a Web Part provider and Web Part consumer.
Change the visibility of the interface to the public by prefixing the keyword public to the interface declaration (see Figure 2). Figure 2. The IProject Interface
int Id { get; } string Name { get; }
In this task, you create a Web Part to participate in a Web Part connection as a provider.
Microsoft.SharePoint.WebPartPages.WebPart, IProject
DropDownList _projectPicker = null; int IProject.Id { get { return int.Parse(_projectPicker.SelectedValue); } } string IProject.Name { get { return _projectPicker.SelectedItem.ToString(); } } 7. Insert the following code in the CreateChildControls subroutine. base.CreateChildControls; try { _projectPicker = new DropDownList(); using (SPSite spSite = new SPSite(SPContext.Current.Web.Url)) using (SPWeb spWeb = spSite.OpenWeb()) { SPList projectsList = spWeb.Lists["Projects"]; foreach (SPListItem project in projectsList.Items) { _projectPicker.Items.Add(new ListItem(project.Name, project.ID.ToString())); } } _projectPicker.AutoPostBack = true; this.Controls.Add(_projectPicker); } catch (Exception ex) { this.Controls.Clear(); this.Controls.Add(new LiteralControl(ex.Message)); }
[ConnectionProvider("Project Name and ID")] public IProject NameDoesNotMatter() { return this; }
In this task, you create a Web Part to participate in a Web Part connection as a consumer.
IProject _provider = null; Label _lbl = null;
base.CreateChildControls; try { _lbl = new Label(); if (_provider != null) { if (_provider.Id > 0) { _lbl.Text = _provider.Name + " was selected."; } else { _lbl.Text = "Nothing was selected."; } } else { _lbl.Text = "No Provider Web Part Connected."; } this.Controls.Add(_lbl); } catch (Exception ex) { this.Controls.Clear(); this.Controls.Add(new LiteralControl(ex.Message)); }
[ConnectionConsumer("Project Name and ID")] public void ThisNameDoesNotMatter(IProject providerInterface) { _provider = providerInterface; }
In this task, you build and deploy the provider and consumer Web Parts. Then you add the Web Parts to a Web Parts page.
Select Custom in the Categories box (see Figure 3), select the ConsumerWebPart, and then click Add. Figure 3. Select the ConsumerWebPart This adds the ConsumerWebPart to the page as shown in Figure 4. Figure 4. The ConsumerWebPart is added to the Web Part zone Repeat these steps to add the ProviderWebPart to a different Web Part zone. Both Web Parts are now displayed on the page as shown in Figure 5. Figure 5. Both Web Parts have been added to the page
In this task, you connect the Provider Web Part to the Consumer Web Part.
You should see the project title displayed in the ConsumerWebPart Web Part.
Figure 6. Change in the Provider Web Part is reflected in the Consumer Web Part
For an in-depth understanding of SharePoint click on
You liked the article?
Like : 0
Vote for difficulty
Current difficulty (Avg): Medium
1/6
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.
Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox