SharePoint: Detect Page Edit or Design Mode in Publishing and Non-Publishing Sites


In your web part or user control, you may wish to know if the page on which your asset is placed is in edit or design mode, in order to alter the behaviour in some way.

How you do this depends on whether you’re using a publishing or non-publishing site.

In a publishing site, you can detect page edit mode simply by using the FormMode property of the FormContext class accessed using the current SPContext as shown below;

var isPublishing = SPContext.Current.FormContext.FormMode != SPControlMode.Invalid;
var wpDMode = WebPartManager.GetCurrentWebPartManager(Page).DisplayMode.Name;
var isEditing = isPublishing
	 ? SPContext.Current.FormContext.FormMode != SPControlMode.Display
	 : (wpDMode.Equals("Edit") || wpDMode.Equals("Design"));

In a non-publishing environment (e.g. Team Sites) the code is a little different depending on whether you set the page in edit mode, or whether you’ve opened a web part property editor.

Firstly, in Team environments, the FormMode property of the FormContext class is always set to Invalid.

Then, you have to get an instance of the WebPartManager class for the current page, and inspect the DisplayMode property.

If the page is in edit mode because you clicked on Edit Page from the ribbon, the DisplayMode property will be set to “Design”.

If the page is in edit mode because you displayed a web part property editor, then the DisplayMode property will be set to “Edit”.


	

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.