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;


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
//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.


Alex Angas said...

Thanks for the post! I'm finding content is hidden when a user is added to the 'viewers' group only. I guess that makes sense from the code. Why the behaviour changed in the first place doesn't make sense...

Alex Angas said...

Actually, looking at the out-of-the-box page layouts, it appears MS intend for this control to no longer be used in display mode. We'll see how that goes in reality!