• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Create a new SharePoint Empty Project for Connectable Web Parts

In this task, you create a SharePoint 2010 Empty project in Microsoft Visual Studio 2010.

To create the SharePoint project

  1. Start Visual Studio 2010, click File, point to New, and then click Project.
  2. Click the Visual C# node in the Installed Templates section, click SharePoint, and then click 2010.
  3. Click the SharePoint Empty Project project template (see Figure 1), provide a name (such as, ConnectTwoWebParts), a location for your project, and then click OK.

Figure 1: Select the Empty SharePoint Project type

 

31

  1. In what local site do you want to use for debugging list, select the site to use (such as http://localhost/SampleWebPage). Also select the Deploy as a farm solution option and then click Finish.

The ConnectTwoWebParts project is created and Solution Explorer is displayed.

Create the Web Part Connection Interface

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.

To create the Web Part connection interface

  1. In Solution Explorer, right-click ConnectTwoWebParts, point to Add, and then click New Item.
  2. In the Add New Item dialog window, click Visual C#, click Code, and then select Interface from the available templates.
  3. Type IProject in the Name box and then click Add.
  4. In Solution Explorer, double-click the cs file.

Change the visibility of the interface to public by prefixing the keyword public to the interface declaration (see Figure 2).

Figure 2. The IProject Interface

32

  1. Insert the following code inside the IProject

int Id { get; }

string Name { get; }

Create the Provider Web Part

In this task, you create a Web Part to participate in a Web Part connection as a provider.

To create the provider Web Part

  1. In Solution Explorer, right-click ConnectTwoWebParts, point to Add, and then click New Item.
  2. Click the Visual C# node in the Installed Templates section, click SharePoint, and then click 2010. Select Web Part from the available templates.
  3. Type ProviderWebPart in the Name box and then click Add.
    The provider Web Part is added to the project.
  4. In Solution Explorer, double-click cs to open the code file.
  5. In the ProviderWebPart class declaration, replace the class from which the ProviderWebPart class is inheriting by changing WebPart to the following:

Microsoft.SharePoint.WebPartPages.WebPart, IProject

 

  1. Insert the following code immediately after the opening bracket ({) in the ProviderWebPart class declaration. This code block implements the IProject Web Part connection interface and adds a local variable to the Web Part.

 

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

 

  1. Insert the following ConnectionProvider property after the CreateChildControls This provides the connection provider interface point for the ProviderWebPart.

 

[ConnectionProvider(“Project Name and ID”)]
public IProject NameDoesNotMatter()
{
return this;
}

  1. Delete the RenderContents subroutine, if it exists.

Create the Consumer Web Part

In this task, you create a Web Part to participate in a Web Part connection as a consumer.

To create the Consumer Web Part

  1. In Solution Explorer, right-click ConnectTwoWebParts, point to Add, and then click New Item.
  2. Click the Visual C# node in the Installed Templates section, click SharePoint, and then click 2010. Select Web Part from the available templates.
  3. Type ConsumerWebPart in the Name box and then click Add.

The consumer Web Part is added to the project.

  1. In Solution Explorer, double-click cs to open the code file.
  2. Insert the following code after the opening bracket ({) of the ConsumerWebPart class declaration.

IProject _provider = null;

Label _lbl = null;

  1. Insert the following code in the CreateChildControls

 

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

  1. Insert the following ConnectionConsumer property after the CreateChildControls This provides the connection consumer interface point for the ConsumerWebPart Web Part.

[ConnectionConsumer(“Project Name and ID”)]
public void ThisNameDoesNotMatter(IProject providerInterface)
{
_provider = providerInterface;
}

 

  1. Delete the RenderContents subroutine, if it exists.

Deploy and Add the Web Parts to a Web Parts Page

In this task, you build and deploy the provider and consumer Web Parts. Then you add the Web Parts to a Web Parts page.

To deploy and add the Web Parts to a Web Parts page

  1. In Solution Explorer, right-click ConnectTwoWebParts and then click Deploy.
  2. Open Internet Explorer and browse to the Web site that you specified for the project.
  3. Click the Site Actions menu and then click More Options.
  4. Scroll and then click Web Part Page. Type SampleWebPartPage in the Name box and then click Create. SharePoint Foundation creates the Web Parts page and opens it in Edit mode.
  5. Click one of the Web Part zones in the blue box.
Desired to gain proficiency on SharePoint? 
Explore the blog post on SharePoint training course to become a pro in SharePoint.

Select Custom in the Categories box (see Figure 3), select the ConsumerWebPart, and then click Add.

Figure 3. Select the ConsumerWebPart

33

This adds the ConsumerWebPart to the page as shown in Figure 4.
Figure 4. The ConsumerWebPart is added to the Web Part zone

34

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

35

 

Connect the Two Web Parts

In this task, you connect the Provider Web Part to the Consumer Web Part.

To connect the Web Parts

  1. In this task, you connect the Provider Web Part to the Consumer Web Part.
  2. Point to Connections, point to Send Project Name and ID To, and then click ConsumerWebPart.

You should see the project title displayed in the ConsumerWebPart Web Part.

  1. Make a different selection in the Provider Web Part list. The change in title is then reflected in the Consumer Web Part zone as shown in Figure 6.

Figure 6. Change in the Provider Web Part is reflected in the Consumer Web Part

 

36

For Indepth understanding of SharePoint click on

 

Summary
Review Date
Reviewed Item
Create a new SharePoint Empty Project for Connectable Web Parts
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Create a new SharePoint Empty Project for Connectable Web Parts"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.