Welcome to MSDN Blogs Sign in | Join | Help

How To: Connect web parts during site provisioning

If you have been following the How To's recently posted here, you will remember that I have a site definition that has the BDC Item web part on it. And I have a custom filter web part that pulls an ID out of the web's property bag. The only remaining item is how to get these two web parts connected during the creation of a site.

Since I used the SharePoint Solution Generator in the WSS Extensions, to capture the site definition, it gives me a hook into an event handler to do exactly that. In the Site Provisioning Handler folder of the VS.NET project it creates, there is a SiteProvisioning.cs file. Here is the code I added to the OnActivated event:

 

public void OnActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;
            SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);
            //filter web part
            System.Web.UI.WebControls.WebParts.WebPart part1 = mgr.WebParts[2];
            //BDC Web Part
            System.Web.UI.WebControls.WebParts.WebPart part2 = mgr.WebParts[3];

            ConsumerConnectionPointCollection ccc = mgr.GetConsumerConnectionPoints(part2);
            ProviderConnectionPointCollection ppp = mgr.GetProviderConnectionPoints(part1);

            TransformableFilterValuesToEntityInstanceTransformer t = new TransformableFilterValuesToEntityInstanceTransformer();

            SPWebPartConnection conn = mgr.SPConnectWebParts(part1, ppp[0], part2, ccc[0], t);
            mgr.SPWebPartConnections.Add(conn);
        }

This code isn't that hard. The trick it to realize that since my custom web part is coded using the ASP.NET 2.0 style, I need to rely on the web part manager class to construct the connection. Based on the position of the web parts in my ONET.XML file, I could get a reference to each of them. Then just grabbed the consumer and provider connection points and built the connection. Since this connection requires the integer ID to be transformed into an entity instance, you need to provide an instance of the appropriate transformer when constructing the connection. In this case, the transformer is the TransformableFilterValuesToEntityInstanceTransformer  class.

It is possible to debug this code. Just have Visual Studio attached before you provision the site. And your breakpoint inside this event will be fired.

Published Tuesday, April 17, 2007 2:46 PM by edhild
Filed under: ,

Comments

# " + title + "

Wednesday, April 18, 2007 2:34 AM by " + title + "

# WSS FAQ additions and corrections LI - 16th - 22nd April 2007

Sunday, April 22, 2007 1:27 AM by Mike Walsh's WSS and more

# WSS FAQ additions and corrections LI - 16th - 22nd April 2007

Sunday, April 22, 2007 1:27 AM by Mike Walsh's WSS and more

# How to connect Web Parts programmatically in WSS 3

Thursday, June 21, 2007 3:10 PM by jason's blog

Web parts are usually connected on the UI as part of customization. There are times better to connect

# 2007 MOSS Resource Links (Microsoft Office SharePoint Server)

Wednesday, September 12, 2007 11:14 AM by The Boiler Room - Mark Kruger, Microsoft SharePoint MVP

2007 MOSS Resource Links (Microsoft Office SharePoint Server) Here is an assortment of various 2007 Microsoft

# 2007 MOSS Resource Links (Microsoft Office SharePoint Server)

Thursday, September 13, 2007 2:16 PM by The Boiler Room - Mark Kruger, Microsoft SharePoint MVP

2007 MOSS Resource Links (Microsoft Office SharePoint Server) Here is an assortment of various 2007 Microsoft

# How to Connect Filters to Web Parts

Sunday, November 18, 2007 3:39 PM by Random Thoughts on SharePoint, BI, and .NET

# How to Connect Filters to Web Parts

Sunday, November 18, 2007 4:30 PM by Noticias externas

# How to connect Filter Web Parts

Sunday, November 18, 2007 6:22 PM by Random Thoughts on SharePoint, BI, and .NET

# SharePoint resources

Monday, February 18, 2008 10:23 AM by SharePoint mola

Gracias a Mark Kruger (SharePoint MVP) por esta lista de recursos de SharePoint donde podréis encontrar

# SharePoint resources

Monday, February 18, 2008 11:10 AM by SHAREPOINTBlogs.com Mirror

Gracias a Mark Kruger (SharePoint MVP) por esta lista de recursos de SharePoint donde podréis encontrar

Anonymous comments are disabled
 
Page view tracker