10

How can one determine if the Template is being published to a site with SiteEdit/XPM enabled?

When Experience Manager is installed, Publication Target in the CME has a flag "Enable for inline editing". When this flag is selected a tab with XPM configuration options appears.

Is it possible to access the value of this flag in a TBB? If not, is there some other way to determine if a given Component or Page is being rendered and published to a SiteEditable location? One of I thought of is by specifying the list of publications having SiteEdit enabled in the parameters of a template. Is there a more elegant way?

2 Answers 2

13

Credit on this solution goes to Eric Huiza.

Here's an Extension Method for the Publication Target that reads the Application Data to check if the target is SiteEditEnabled:

public static bool IsSiteEditEnabled(this PublicationTarget target)
{
    try
    {
        if (target == null)
            return true;
        ApplicationData appData = target.LoadApplicationData("SiteEdit");
        string data = Encoding.UTF8.GetString(appData.Data);
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(data);

        XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());
        nsManager.AddNamespace("se", "http://www.sdltridion.com/2011/SiteEdit");

        XmlNode seNode = xmlDoc.SelectSingleNode("/se:configuration/se:PublicationTarget/se:EnableSiteEdit",
                         nsManager);
        if (seNode != null && seNode.InnerText.Equals("true"))
            return true;
        return false;
    }
    catch (Exception)
    {
        // Quick and dirty fix
        return false;
    }
}

Add this to your Template project, and then you can do something along these lines:

if(engine.PublishingContext.PublicationTarget.IsSiteEditEnabled())
    // do something for SE enabled targets

EDIT

With SDL Web 8 you can determine this by using TargetType.IsPreviewCapable(Publication) - see example here.

2
  • This might not work on Web 8 using Business Process Types! (I have not tested this!) Commented Aug 11, 2016 at 16:01
  • 2
    Indeed, with Web 8 the approach is different. You should use TargetType.IsPreviewCapable - see "IsXpmEnabled" method in github.com/sdl/dxa-content-management/blob/…
    – Nuno Linhares
    Commented Aug 11, 2016 at 16:10
8

If you are using Razor Mediator to build your layout you can use its built-in function IsSiteEditEnabled. The Razor Mediator v1.3.3 documentation has the following example.

@if (IsSiteEditEnabled) {

<div>Component Title: @Component.Title</div>

}

1
  • Thanks for answering and welcome to the community, Les Dancsecs! Commented Oct 15, 2015 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.