Donald Hessing

Webpart connections - data provider / data consumer

Ever needed a solution where several webparts on the same page are consuming the same datasource? Webpart connections can be the solution!

In one of my current projects we have a profile page with several web parts were each webpart shows a part of the user profile information. For instance:

 - a webpart with the basic profileinformation like name, phone and profilepicture.

 - a webpart for showing the favorite links stored in the userprofile

 - a webpart for showing the skills of the user, stored in the userprofile

 

In this scenario, each webpart has to retrieve the same userprofile information. This is a little bit sad considered the performance penalty being paid for retrieving the same data for each webpart. Considered the penalty, i wanted a solution which only loads the userprofile once.

In a traditional Asp.Net website, the code behind of the page would be responsible for retrieving the data and pass that data to the webparts by using a public property on the webpart. SharePoint publishing pages don’t have the ability to have code behind files. So we need a different kind of solution.

I could created a dataaccess layer for retrieving the userprofile data and cache the profile for several seconds so that the other webparts could read this data from the cache.

Just before writing the dataaccess and caching code, i wondered whatever it was possible to achieve this by using webpart connections. See it like data provider webpart and a data consumer webpart.

To keep it short. I came up with the following solution.

I created a custom webpart witch implements the IUserProfileData interface. This interface defines the contract of the data being send through the webpart connections.

 public class UserProfileData

 {

        public string FistName { get; set; }

        public string LastName{ get; set; }

        public string PictureUrl { get; set; }

 }

 

 public interface IUserProfileProvider

 {

        UserProfileData ProfileData{ get; }

 }

 

The easiest way to communicate between webparts is by using the ConnectionProvider and the ConnectionConsumer attribute. All you need to do in the provider webpart is implementing a method with the ConnectionProvider attribute. The webpart infrastructure will deal with the communication callbacks between the webparts

[ConnectionProvider("ProviderID")]

public IUserProfileProvider GetUserProfileProvider()

{

       return this;

}

 

All the consumer webpart has to do is implement a method with the ConnectionConsumer attribute for retrieving the provider.

[ConnectionConsumer("ConsumerID")]

public void RegisterUserProfileProvider(IUserProfileProvider provider)

{

this.provider = provider;

}

 

The final step is to create a webpart connection between the provider and the consumer webparts. This can be done by using the SharePoint userinterface.

Example code can be downloaded here. If you want to deploy the example, make sure you have STSDEV(can be found on codeplex) installed. Want to know more about webpart connections? Read this excellent article on MSDN (http://msdn.microsoft.com/en-us/library/ms178187.aspx).

 

Comments

Dennis van der Stelt said:

Wow, a blogpost! I'm honored! ;)

If you upload the file into the media section, you can track views and downloads.

# October 10, 2009 10:47 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 5 and 1 and type the answer here: