Working with different view modes in EPiServer Composer

by Fredrik Karlsson 14. October 2009 21:23

Introduction

When building a function in EPiServer Composer, there might be times when you have different behaviour in edit mode and in view mode. This article will demonstrate how to determine what view mode you are in.

What is a view mode

View modes is simply composers way of determining if you are, for example editing or viewing a page. If you are editing a page, then Composer will attach all necessary code to be able to drag/drop and edit the function, but if you are viewing the page, then Composer won't add anything.
There are 6 different view modes, see them all with descriptions in the Composer SDK about view modes.

Implementation

The current view mode is displayed in the query string, parameter DE_VM. (DE_VM stands for DropitExtension_ViewMode.) Each view mode has a value assigned to it:

  • ExtensionUndefinedMode = 0
  • ExtensionNormalMode = 1
  • ExtensionAdminMode = 2
  • ExtensionEditMode = 4
  • ExtensionEditOnPageMode = 8
  • ExtensionSpecificVersionViewMode = 16
  • So a request to the url http://exampe.com/default.aspx?id=3&DE_VM=4 will show the page in "Composer - edit on page" mode (if you have enough access rights, that is).

    Determining the view mode

    BaseContentFunction.ViewMode


    A very fast way. You simply get back an Enum-value on wich view mode you are in.

    public partial class MyFunction : Dropit.Extension.Core.BaseContentFunction
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.ViewMode == ExtensionGeneric.ViewMode.ExtensionEditOnPageMode)
            {
                // do stuff
            }
            else if (this.ViewMode == ExtensionGeneric.ViewMode.ExtensionNormalMode)
            {
                // Do other stuff
            }
        }
    }
    

    BaseContentFunction.IsEditMode

    A helper function, it simply checks if the view mode is ExtensionEditMode or ExtensionEditOnPageMode.

    public partial class MyFunction : Dropit.Extension.Core.BaseContentFunction
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsEditMode)
            {
                // do stuff
            }
            else
            {
                // Do other stuff
            }
        }
    }
    

    Dropit.Extension.Common.Utils.GetViewMode( System.Web.HttpContext context )

    This is a way to get the view mode in any other context. Simply pass the context to the method and you will recieve the ViewMode

    public partial class MyFunction : Dropit.Extension.Core.BaseContentFunction
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Dropit.Extension.Common.Utils.GetViewMode( this.Context) == ExtensionGeneric.ViewMode.ExtensionEditOnPageMode)
            {
                // do stuff
            }
            else
            {
                // Do other stuff
            }
        }
    }
    

    Tags: , , ,

    Development

    blog comments powered by Disqus

    Creative Commons License
    This work is licensed under a Creative Commons Attribution-Share Alike 2.5 Sweden License.


    Welcome to the Dropit blog!

    Here we, the people that work at Dropit, will write about stuff that interests us. For example web development, especially with .NET and EPiServer - but we'll also talk about other techniques that interest us, marketing on the web, social phenomenons, pop culture, games and software development in general.