Wednesday, August 11, 2010

Connect webparts programmatically using SPConnectWebParts

If you wish to connect two webpart where you have webpart consumer and webpart provider object then you can use following idea to implement functionality.

========================================================================
private void ConnectWebPartOnPage(SPWeb oWeb, WebPart webPartConsumer, WebPart webPartProvider, string MappingId)
{
SPFile file = null;
SPLimitedWebPartManager manager = null;
file = oWeb.GetFile(strPageUrl);

try
{
if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
file.CheckOut();
manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);

ConsumerConnectionPoint consumerConnection = null;
foreach (ConsumerConnectionPoint point in manager.GetConsumerConnectionPoints(webPartConsumer))
{
if (point.InterfaceType == typeof(IFilterValues))
{
consumerConnection = point; break;
}
}
ProviderConnectionPoint providerConnection = null;
foreach (ProviderConnectionPoint point in manager.GetProviderConnectionPoints(webPartProvider))
{
if (point.InterfaceType == typeof(Microsoft.SharePoint.WebPartPages.ITransformableFilterValues))
{
providerConnection = point; break;
}
}

TransformableFilterValuesToFilterValuesTransformer transformMapping = new TransformableFilterValuesToFilterValuesTransformer();
transformMapping.MappedConsumerParameterName = MappingId;
manager.SPConnectWebParts(webPartProvider, providerConnection, webPartConsumer, consumerConnection, transformMapping);

oWeb.Update();
}
}
catch (Exception ex)
{
// exception here
}
finally
{
if (manager != null)
manager.Dispose();

if (file != null)
{
file.CheckIn("Connected web parts, if any");
file.Publish("Connected web parts, if any");
}
}
}
===============================================================================

this above function can be use for following points:-


Programmatically connect a Consumer WebPart & a Provider WebPart

or

How to connect WebParts Programmatically?

or

Connecting webparts

or

use of ConsumerConnectionPoint and ProviderConnectionPoint

or

use of SPConnectWebParts

or

How to connect Web Parts programmatically in WSS 3


Thanks
Avinash Dad

6 comments:

aksneo said...

HI Avinash,

great post !! >> By any chance were you working with Pfizer (Wipro) in the NY city..
I am Akshay who work in pfieldnet application if u remember me.
If not, sorry it was the wrong person.

Avinash Maheshwari said...

Thanks..
You are right.
I was working with wipro...
I could not recognize you..

Avinash

Anonymous said...

Your solution is incomplete!
What is MappingID? How do you build the string to pass to?

Avinash Maheshwari said...

Are you doing export import webpart of a page? I would like to know your goal. This will help me to give answer.
If you are doing export import then while export you can use below code to find mapping id:-

Note:- SiteWebPart is my own class.

private List FindConnectedWebPart(SPLimitedWebPartManager wpm, List SiteWebPartClasss)
{
SPWebPartConnectionCollection webPartConColl = wpm.SPWebPartConnections;
try
{
foreach (SPWebPartConnection webPartCon in webPartConColl)
{
string strProviderTitle = RemoveSpecialCharacters(webPartCon.Provider.Title);
string strConsumerTitle = RemoveSpecialCharacters(webPartCon.Consumer.Title);
SiteWebPartClass webpartSetting = SiteWebPartClasss.Find(e => e.Settings.Title == strProviderTitle);
TransformableFilterValuesToFilterValuesTransformer transformerMapping = (TransformableFilterValuesToFilterValuesTransformer)webPartCon.Transformer;

if (webpartSetting != null)
{
webpartSetting.Settings.ConsumerWebPart = strConsumerTitle;
webpartSetting.Settings.ProviderWebPart = strProviderTitle;
webpartSetting.Settings.MappingId = transformerMapping.MappedConsumerParameterName;

}
}
}
catch (Exception ex)
{
// exception
}
return SiteWebPartClasss;
}

Hope this help you.

Anonymous said...

I'm also wondering what is MappingID !?
I'm trying to connect a custom webpart implementing ITransformableFilterValues with a BusinnesDataListWebpart

Thx

Avinash Maheshwari said...

Hi,

I am doing export import of webparts...
So while exporting webpart i get mapping ID also.
Please refer my this article for detail:-
Export and import webparts programmatically


Or search "Export and import webparts programmatically" on sharepointdad.wordpress.com...

Now if you are not exporting but still wish to connect new webparts then this below solution might work for you



// connect the webpartsmanager.SPConnectWebParts(providerPart, providerConnectionPoint, consumerPart, consumerConnectionPoint, transformer);

where transformer can get like this:-
SPRowToParametersTransformer transformer = new SPRowToParametersTransformer();transformer.ProviderFieldNames = new string[] { "LinkTitleNoMenu" };transformer.ConsumerFieldNames = new string[] { "ArticleEditionTitle" };