Detecting the current mode (display/edit)

23 09 2008

One of these days I came accross a simple requirement that led me to a couple of scenarios: “How do I detect if I am in display mode or in edit mode?”. This has some significant importance in WCM (Web Content Management) world, where you may need some tricks to hide fields, controls or even alter the look of a component. Thhis can be achieved (at least) in three ways:

  • in the Layouts (aspx) code only:

In this case you have specific zones where to put controls that appear in edit mode and in display mode (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.webcontrols.editmodepanel.aspx):

For any part you need to hide from Edit mode surround it by

<PublishingWebControls:EditModePanel runat=server id="EditModePanel1" PageDisplayMode="Display">
<!-- your code here -->
</PublishingWebControls:EditModePanel>

For any part you need to hide from Display mode surround it by

<PublishingWebControls:EditModePanel runat=server id="EditModePanel1" PageDisplayMode="Edit">
<!-- your code here -->
</PublishingWebControls:EditModePanel>
  • code behind: webparts

In this case, the object responsible for the information of the current mode is the WebPartManager.DisplayMode (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webpartmanager.displaymode.aspx). The allowued values for this enumerate are (from msdn):

 

Display mode Description
BrowseDisplayMode Displays Web Parts controls and UI elements in the normal mode in which end users view a page.
DesignDisplayMode Displays zone UI elements and enables users to drag Web Parts controls to change the layout of a page.
EditDisplayMode Displays special editing UI elements and enables end users to edit the controls on a page.
CatalogDisplayMode Displays special catalog UI elements and enables end users to add and remove page controls.
ConnectDisplayMode Displays special connections UI elements and enables end users to connect Web Parts controls.

Note that this only works correctly if there is only 1 webpart on the page as the test is done individually.

  • code behind: other situations

This is probably the least used mode, nevertheless, I had to used a couple of times. You can detect the current mode through:

Microsoft.SharePoint.SPContext.Current.FormContext.FormMode

This is an enumerate with the values:

  • SPControlMode.Display
  • SPControlMode.Edit

Thus you can build a very simple test:

  if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Display)
  {
   // your code to support display mode
  } 
  else // Microsoft.SharePoint.SPContext.Current.FormContext.FormMode = SPControlMode.Edit
  {
   // your code to support edit mode
  }
  
About these ads

Actions

Information

8 responses

30 05 2009
Lee Dale

Nice one, I was racking my brains trying to work this out.

9 09 2009
Luke Watson

Thanks for that, I forgot how to do it, and it was driving me crazy.

14 12 2009
In Development

[...] it isn’t mine, credits go to this post. Categories: C#, SharePoint Tags: SharePoint, WebPart Comments (0) Trackbacks (0) Leave a [...]

30 07 2010
Koen Zomers

Thanks, exactly what I needed! In SharePoint 2010 the enum has been expanded to contain the following values:

– Display
– Edit
– New
– Invalid

20 02 2011
Roman

Hello! There is some problem to receive current control mode in OnInit method when you create a new item. Here http://dotnetfollower.com/wordpress/2010/12/sharepoint-get-spcontrolmode-which-is-associated-with-the-current-request/ I described how to receive the current control mode as earlier as possible, even inside OnInit.

11 05 2011
Dilip

Thanks for sharing this article. My custom JS gives me an error in edit mode. Now its resolved.

Thanks again….. :)

23 01 2012
Alan Coulter

Note that in my testing, SPContext.Current.FormContext.FormMode returns invalid for meeting workspaces in both edit and display mode. I suspect that there must be another way to determine the mode in SharePoint Foundation.

31 05 2012
How to use the same field multiple times in a Sharepoint 2010 page layout ? — Çağatay Kalan's Weblog

[...] contents based on the current display mode of the page. To do this, you use the  EditModePanel. This blog post describes how to use this [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

Join 430 other followers

%d bloggers like this: