Title Bar Properties Link Does Not Work on Dynamically Created Pages

Ran into a problem recently while helping out on a project where web part pages were being dynamically created from a feature receiver.  Once the pages were created, everything works, except for the Title Bar Properties icon in the ribbon.

image

When you click on the Title Bar Properties icon, nothing happens.  If we create a page manually via the browser, the icon would work.  Comparing the rendered HTML for a working/failing pages shows that the working page has an anchor tag with JavaScript to show the properties tool pane.

    1: <a href="javascript:MSOTlPn_ShowToolPane2Wrapper('Edit', 16, 'g_6efbf127_414f_4f2c_b51a_c1af6a064b1e')" id="EditTitleBar" style="display:none;">
    2:     Edit Title Bar Properties
    3: </a>

I was able to track this JavaScript down to the TitleBarWebPart class.  Thinking that this control was dynamically added to the page, I used WinDBG.exe to see if there were any exceptions being thrown resulting in the control not adding the anchor tag and script.  I confirmed the page was not throwing any exceptions, and went even further to confirm that the constructor of the TitleBarWebPart class was not being called.  This confirmed that the failing page is not even trying to load the control that provides the functionality.

I then opened up SharePoint Designer 2010 to compare the two pages, and search for the TitleBarWebPart.  Found it!  Right where someone left it…in the web part page, in a WebPartZone, aptly identified as TitleBar.

    1: <WebPartPages:WebPartZone runat="server" title="loc:TitleBar" id="TitleBar" AllowLayoutChange="false" AllowPersonalization="false"><ZoneTemplate>
    2:     <WebPartPages:TitleBarWebPart runat="server" AllowEdit="True" 
    3:         AllowConnect="True" ConnectionID="00000000-0000-0000-0000-000000000000" 
    4:         Title="Web Part Page Title Bar" IsIncluded="True" Dir="Default" 
    5:         IsVisible="True" AllowMinimize="False" ExportControlledProperties="True" 
    6:         ZoneID="TitleBar" ID="g_6efbf127_414f_4f2c_b51a_c1af6a064b1e" HeaderTitle="Test" 
    7:         AllowClose="False" FrameState="Normal" ExportMode="All" AllowRemove="False" 
    8:         AllowHide="True" SuppressWebPartChrome="False" DetailLink="" ChromeType="None" 
    9:         HelpLink="" MissingAssembly="Cannot import this Web Part." PartImageSmall="" 
   10:         HelpMode="Modeless" FrameType="None" AllowZoneChange="True" PartOrder="2" 
   11:         Description="" PartImageLarge="" IsIncludedFilter="" __MarkupType="vsattributemarkup" 
   12:         __WebPartId="{6EFBF127-414F-4F2C-B51A-C1AF6A064B1E}" WebPart="true" Height="" Width="">
   13:     </WebPartPages:TitleBarWebPart>
   14:  
   15: </ZoneTemplate></WebPartPages:WebPartZone>

In order to get the Title Bar Properties link to work on the dynamically created web part pages, we created a function in the Feature Receiver that takes in the SPLimitedWebPartManager for a web part page, and adds the TitleBarWebPart to the TitleBar web part zone.  Notice that I’m not setting all the declared properties programmatically to keep the example short, but feel free to set whatever properties you want.

    1: private void AddTitleBarWebPart(SPLimitedWebPartManager webPartManager) 
    2: { 
    3:     TitleBarWebPart titleWebPart = new TitleBarWebPart(); 
    4:     webPartManager.AddWebPart(titleWebPart, "TitleBar", 0); 
    5: }