Saturday, June 12, 2010

Inconvenient EditmodePanel Control

You can use the EditModePanel control in a publishing scenario to control the visibility of other controls in relation to the display mode. With other words – it acts like a container that shows or hides its child controls based on the mode of the page. You can use the PageDisplayMode property to indicate whether this control is visible in Edit Mode or Display Mode.

<PublishingWebControls:EditModePanel id="EditModePanel1"
runat="server" PageDisplayMode=”Display”/>

I frequently embed two EditModePanels into my pagelayout. One with its PageDisplayMode ‘Display’ and one with ‘Edit’. This way you have a styled and an non-styled presentation of your field controls. The styled presentation will be shown only in display mode. The non-styled presentation will be only used in edit mode.

While this works great in SP 2007 – I discovered that this control behaves a little different in SP 2010. I discovered that it only works as expected if you’re an authenticated user. So, what’s the difference? Well… I noticed that when you set the PageDisplayMode property to “Display” – The child controls will never be shown for anonymous users.

I was wondering why this is the case in SP 2010, while this works great in SP 2007, so I investigated the EditModePanel class in the Microsoft.SharePoint.Publishing assembly and found the evil-doer. MS added an extra check in the CalculateShouldRender method, which prevent rendering for anonymous users:

if (!ConsoleUtilities.CheckPermissions(SPBasePermissions.EditListItems))
{
this.shouldRender = false;
}

Workaround

Create your own Panel webcontrol and write the exact same logic as the EditModePanel control – without that little check. You can check the form mode like this
if (SPContext.Current.FormContext.FormMode == SPControlMode.Display)
{
//display mode
}
else
{
//edit mode
}

Attention: Don’t write your logic in the overridden Render method, use the AddParsedSubObject method instead. If you use the Render method, you will have some problems with the Ribbon.