Connecting webparts at Object Model

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.

Advertisements

2 Responses to Connecting webparts at Object Model

  1. Manoj Iyer says:

    Thats a very informative post. I am also facing a similar problem. I had connectable webparts on the default.aspx page of my site. Now to place the same in the production environment, site defination is beeing used. As per you post I will try the same and get back. If you have any updates please let me know.

    Thanks in advance.

  2. Jim Adcock says:

    Did you ever find a solution to this? I have encountered a similar peoblem.

    I’m creating a automated provisioning solution. Users fill out a form thad defines certain characteristics of the site. Upon approval, an event handler creates the site and makes modifications.

    One of the modifications is to the Audience setting of a task list web part. But when that code is run, another web part on the page, which links to a KPI list, gets unconfigured – it no longer successfully points to the list, and its title changes to “Untitled”.

    Interestingly, in my case it is not the web part that is being moified that is getting its settings re-initialized to empty values.

    Still looking for my solution, but I’ll post here if I figure it out. For now, I think I am going to try to add code after the other web part modification to put the web part’s settings back.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: