Connecting webparts at Object Model

February 8, 2008

Unfortunately there is no way in Sharepoint 2007 you can connect two webparts in a site definition configuration file (such as ONET.xml). I can’t understand why such a feature isn’t provided by the platform since is something you usually need in custom site definitions.

I’m currently developing a custom site definition for an e-Learning portal. One of the pages of the site is composed by a student search filter webpart and a student result list webpart which should be connected as a provider (filter) and consumer (list) of the search criteria of the current search. Obviously, I want this connected to be deployed when the administrator creates a new site of this site definition template however, as I pointed before, such a feature is not available at any configuration file (onet.xml or a feature element) of the site definition.

Researching on this topic, I came up with what seemed a way to solve this issue: implement the webpart connection through Object Model at a site creation event of a site provisioning assembly or a feature receiver class.

The only information I found on this topic was at the following posts by jasonwj and edhilds which suggested that this could be done with a SPLimitedWebPartManager object. Msdn documentation on this topic, although almost inexistent, seems to suggest also that.

However, I didn’t manage to get the expected results. Following is the code I wrote at the FeatureActivated event of the FeatureReceiver class:

SPWeb web = (SPWeb)properties.Feature.Parent;
SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager("search_students.aspx", PersonalizationScope.Shared);

System.Web.UI.WebControls.WebParts.WebPart searchFilter = mgr.WebParts[0];
System.Web.UI.WebControls.WebParts.WebPart searchResults = mgr.WebParts[1];

ProviderConnectionPoint searchFilterConnPoint = mgr.GetProviderConnectionPoints(searchFilter)["SearchProvider_ot"];
ConsumerConnectionPoint searchResultsConnPoint = mgr.GetConsumerConnectionPoints(searchResults)["SearchConsumer_ot"];

SPWebPartConnection conn = mgr.SPConnectWebParts(searchFilter,
                                       		 searchFilterConnPoint,
                                                 searchResults,
                                                 searchResultsConnPoint);

Both webparts where build from this msdn example just for testing purposes.This code didn’t connect both webparts when the feature was activated, apparently nothing seemed to happen but the consumer webpart (searchResults) got all it’s properties reseted and initialized to empty values (for example, it’s title property was set to “Untitled”). Apart from that, nothing else seemed to happen.I’m still researching on this topic, unfortunately, there is not much about it. If anyone has any point on how could we connect two webparts in a site definition or what is happening with my implementation please leave a comment ! I can also give the source code of the feature if you want to try it at your computer.