Developer FAQ

I want to emit special output from my code when it's running in a page preview - how do I detect if my ASP.NET control is executing in public or preview mode?

Answer:

The following code can be used in C# functions and user controls to create data scope depending scenarios:

using System;
using Composite.Data;

public partial class Frontend_Example_CheckMode : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (DataScopeManager.CurrentDataScope.Equals(DataScopeIdentifier.Administrated) == true)
        {
            Label1.Text = "Preview mode: Visible for admins only!";
        }

        if (DataScopeManager.CurrentDataScope.Equals(DataScopeIdentifier.Public) == true)
        {
            Label1.Text = "Public mode: Visible for all users!";
        }
    }
}

Please do the following steps to test the code provided:

  1. Download and unpack CheckMode.ascx and CheckMode.ascx.cs to Frontend/Example/ folder.
  2. Create a page, switch to the "Content" tab, click the "Source" icon and insert this code:
    <f:function name="Composite.AspNet.LoadUserControl" xmlns:f="http://www.composite.net/ns/function/1.0">
      <f:param name="Path" value="~/Frontend/Example/CheckMode.ascx " />
    </f:function>
  3. Save the page and click "Preview" icon: you'll get this message: "Preview mode: Visible for admins only!"
  4. Publish page and open this page on the site: you'll get this message: "Public mode: Visible for all users!"