Thursday, July 29, 2010

View mode panel for Sharepoint

If you used publishing infrastructure in Sharepoint then you probably know OTB EditModePanel control defined in Microsoft.SharePoint.Publishing.dll assembly. Using this control you can define what content on your publishing page  should be shown only in edit mode:

   1: <PublishingWebControls:EditModePanel runat="server">
   2:     ...
   3: </PublishingWebControls:EditModePanel>

In this example everything inside EditModePanel will be shown only when user modifies page. In view mode it will not be shown.

Unfortunately there is no similar OTB ViewModePanel which shows content only when page is running in view mode, i.e. when user views the page. So I investigated code of OTB EditModePanel via Reflector and created following control:

   1: public class ViewModePanel : Panel
   2: {
   3:     private bool shouldRender;
   4:  
   5:     protected override void AddParsedSubObject(object obj)
   6:     {
   7:         this.calculateShouldRender();
   8:         if (this.shouldRender)
   9:         {
  10:             base.AddParsedSubObject(obj);
  11:         }
  12:     }
  13:  
  14:     private void calculateShouldRender()
  15:     {
  16:         this.shouldRender = false;
  17:  
  18:         if (SPContext.Current.FormContext != null)
  19:         {
  20:             this.shouldRender =
  21:                 (SPContext.Current.FormContext.FormMode ==
  22:                     SPControlMode.Display);
  23:         }
  24:         this.Visible = this.shouldRender;
  25:     }
  26: }

Now we are able to specify it in layout of publishing pages:

   1: <uc:ViewModePanel runat="server">
   2:     ...
   3: </uc:ViewModePanel>

and all content between begin and end tags will be shown only in view mode.

2 comments:

  1. how do you create the user control?
    this one is a web control, in facts inherits from panel!

    ReplyDelete
  2. Marco,
    it is not user control - it was implemented as custom control, as it doesn't require complicated visual layout

    ReplyDelete