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.

Comments (11)

  1. jason's blog says:

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

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

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

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

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

Skip to main content