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
}
Nice one, I was racking my brains trying to work this out.
Thanks for that, I forgot how to do it, and it was driving me crazy.
[...] it isn’t mine, credits go to this post. Categories: C#, SharePoint Tags: SharePoint, WebPart Comments (0) Trackbacks (0) Leave a [...]
Thanks, exactly what I needed! In SharePoint 2010 the enum has been expanded to contain the following values:
- Display
- Edit
- New
- Invalid
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.
Thanks for sharing this article. My custom JS gives me an error in edit mode. Now its resolved.
Thanks again…..
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.
[...] 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 [...]