Search This Blog

2011-02-07

Create a connected web part and activate through feature

Step 1-Create a class library project and make the folder structure as follows


Step 2-Connectedwebpart.cs will be as follows
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace ConnectedWebPart
{
public interface ICustomerProvider
{
string CustomerID { get; }
string CustomerName { get; }
}
public class SimpleProviderExample : WebPart, ICustomerProvider
{
private string _customerID = "P1284";
private string _customerName = "Manab";

protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("Customer ID: " + this.CustomerID);
writer.Write("Customer Name: " + this.CustomerName);
}
public string CustomerID
{
get { return this._customerID; }
}
public string CustomerName
{
get { return this._customerName; }
}

[ConnectionProvider("Customer ID","Customer Name", AllowsMultipleConnections = true)]
public ICustomerProvider GetCustomerProvider()
{
return this;
}
}

public class SimpleConsumerExample : WebPart
{
private ICustomerProvider customerProvider;

[ConnectionConsumer("Customer ID", "Customer Name")]
public void RegisterCustomerProvider(ICustomerProvider provider)
{
this.customerProvider = provider;
}

protected override void RenderContents(HtmlTextWriter writer)
{
if (this.customerProvider != null)
{
writer.Write(this.customerProvider.CustomerID);
writer.Write(this.customerProvider.CustomerName);
}
else
writer.Write("No connection");
}
}
public class ConnectionWebPart
{
}
}

Step 3-SimpleConsumerExample.webpart will be as follows-
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="ConnectedWebPart.SimpleConsumerExample" />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="AllowClose" type="bool">True</property>
<property name="Width" type="unit" />
<property name="AllowMinimize" type="bool">True</property>
<property name="AllowConnect" type="bool">True</property>
<property name="ChromeType" type="chrometype">Default</property>
<property name="TitleIconImageUrl" type="string" />
<property name="Description" type="string" />
<property name="Hidden" type="bool">False</property>
<property name="TitleUrl" type="string" />
<property name="AllowEdit" type="bool">True</property>
<property name="Height" type="unit" />
<property name="HelpUrl" type="string" />
<property name="Title" type="string">SimpleConsumerExample</property>
<property name="CatalogIconImageUrl" type="string" />
<property name="Direction" type="direction">NotSet</property>
<property name="ChromeState" type="chromestate">Normal</property>
<property name="AllowZoneChange" type="bool">True</property>
<property name="AllowHide" type="bool">True</property>
<property name="HelpMode" type="helpmode">Navigate</property>
<property name="ExportMode" type="exportmode">All</property>
</properties>
</data>
</webPart>
</webParts>

Step 4-SimpleProviderExample.webpart will be as follows-
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="ConnectedWebPart.SimpleProviderExample" />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="AllowClose" type="bool">True</property>
<property name="Width" type="unit" />
<property name="AllowMinimize" type="bool">True</property>
<property name="AllowConnect" type="bool">True</property>
<property name="ChromeType" type="chrometype">Default</property>
<property name="TitleIconImageUrl" type="string" />
<property name="Description" type="string" />
<property name="Hidden" type="bool">False</property>
<property name="TitleUrl" type="string" />
<property name="AllowEdit" type="bool">True</property>
<property name="Height" type="unit" />
<property name="HelpUrl" type="string" />
<property name="Title" type="string">SimpleProviderExample</property>
<property name="CatalogIconImageUrl" type="string" />
<property name="Direction" type="direction">NotSet</property>
<property name="ChromeState" type="chromestate">Normal</property>
<property name="AllowZoneChange" type="bool">True</property>
<property name="AllowHide" type="bool">True</property>
<property name="HelpMode" type="helpmode">Navigate</property>
<property name="ExportMode" type="exportmode">All</property>
</properties>
</data>
</webPart>
</webParts>

Step 5-feature.xml will be as follows-
<Feature
Id="FE016E00-8639-4839-925D-B40F659458A9"
Title="Sample Connected Web Parts"
Description="This demoware was created for Inside Windows SharePoint Services (Pattison/Larson)"
Hidden="FALSE"
Scope="Site"
ImageUrl="TPG/canteen.gif"
xmlns="http://schemas.microsoft.com/sharepoint/">

<ElementManifests>
<ElementManifest Location="elements.xml"/>
</ElementManifests>

</Feature>

Step 6-elements.xml will be as follows-
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<Module Name="LitwareWebParts"
List="113"
Url="_catalogs/wp"
Path="dwp"
RootWebOnly="true">
<File Url="SimpleConsumerExample.webpart" Type="GhostableInLibrary" >
<Property Name="Group" Value="Litware Web Parts" />
</File>
<File Url="SimpleProviderExample.webpart" Type="GhostableInLibrary" >
<Property Name="Group" Value="Litware Web Parts" />
</File>
</Module>
</Elements>

Step 7-Copy the dll to GAC and others files to the respective folder of 12 hives

Step 8-Make a safecontrol entry to the web config of your site
<SafeControl Assembly="ConnectedWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30534ee939de1c1d" Namespace="ConnectedWebPart" TypeName="*" Safe="True" />

Step 9-Activate your feature from the site collection features


Step 10-Add it to your web part zone and make the connection between two web parts

No comments: