Web Parts are an important and everywhere used part of SharePoint. Web Parts are documented pretty good and there are several sources describing as just ASP.NET WebParts (Darren Neimke book) and some resources about Web Parts in SharePoint as well. But in reality SharePoint WebPart specific stuff is not properly documented, and in this post I’d like to publish some tips regarding Web Parts in SharePoint
Not all Web Parts are inherited from System.Web.UI.WebControls.WebParts.WebPart class. Some of them comes from Microsoft.SharePoint.WebPartPages.WebPart, which is inherited from the System.Web.UI.WebControls.WebParts.WebPart now. When you develop your own Web Parts, in mostly cases you need to inherit from System.Web WebPart class
Finding the connections
For some tasks you might handle the Web Part connection/disconnection programmatically. For example, connect your Web Part via code or via Page Layouts. To do this you need to know the Consumer and Provider names. It’s not the problem for your Web Parts, but how to find the Provider name for the out-of-the-box Web Parts?! For this you need to use Reflector.
First, to know the class name of SharePoint Web Part you need just put any Web Part to the page and export it to XML. In that XML you will find the assembly and class name. After that open that assembly in Reflector and navigates to the class you need to get Provider name. Scrutinize that Web Part class to find the connections and name of Provider connection. Connection may be not exactly in that class, but in one of the base classes.
For example for Page Filter Web Parts the Provider name is “ITransformableFilterValues”
Deploying Web Parts with connections
If you deploy you pages and want to have Web Parts being already connected for each new page user creates, then you need to connect Web Part via XML description inside Page Layout.
Connection consists from 3 parts:
- Consumer ID and Name –which you know, because usually it’s you own custom Web Parts
- Provider ID and Name – I described above how to extract that name
- Name of the parameter you select from dialog box when connect Web Parts manually.
So, let’s start:
- Put any Web Parts to your Page Layout (I use SharePoint Designer for this, because it simplify my work), save page and navigate to the code.
- In code find the <WebPartPages:WebPartZone> section, and there you will find you Web Parts markup tagged with <Components: prefix.
- Find the ProviderID attribute of the Web Parts you want to connect and remember values . These values will be used when we create the connection between Web Parts.
- Open your page in browser to verify it works.
- Connect Web Parts manually, and save the name of the parameter which is shown in the popup dialog box when you connect Web Parts
- Remove connections between Web Parts, and return back to SharePoint designer
- Open the Page Layout code and navigate to the very beginning of document. Try to find the following section “<WebPartPages:SPProxyWebPartManager”. If you don’t have this section you can miss the following step
- Add the <WebPartPages:SPProxyWebPartManager runat="server" ID="ProxyWebPartManager"> section inside “PlaceHolderMain” content holder
- Now, we are ready to create connections. Add the following codesnippet inside SPProxyWebPartManager
2: <WebPartPages:SPWebPartConnection ID="g_myConnectionName"
3: ConsumerConnectionPointID="IFilterValues" ConsumerID="WebPartA"
4: ProviderConnectionPointID="ITransformableFilterValues" ProviderID="WebPartB">
5: <WebPartPages:TransformableFilterValuesToFilterValuesTransformer MappedConsumerParameterName="Parameter">
Let’s review the “blue” selections, because it’s where connection magic happens
- g_myConnectionName is the name of current connection, can be any name
- IFilterValues – Consumer interface name of Web Part which accept connection
- WebPartA – consumer Web Part ID from the WebPartZone section we found and remembered previously
- ITransformableFilterValues – Provider interface name of Web Part which provides connection (in this sample I used Page Filter web parts and they have such provider name. Refer to the “Finding the connections” section where I described how to extract the names
- WebPartB – provider Web Part ID from the WebPartZone section we found and remembered previously
- Parameter – the name from the connection dialog box where you select the parameter
10. Save the page layout. Go to SharePoint and create the new page based on that page layout. After page will be created you can find that your Web Parts are already connected. Job done!
Take into account, that you can’t change Web Parts connection for pages created for the Page Layouts with predefined Web Part connections.