Skip to main content

Implementing Experience Manager in DD4T 2 .NET

This post describes an implementation of Experience Manager (XPM) for DD4T 2 .NET using the CIL (REST) providers in SDL Web 8. The implementation would be basically the same in a more standard architecture where the Tridion Content Delivery stack would be used.

XPM Configuration Switch

In you web application's web.config, add the following line inside the configuration / appSettings node:

    <add key="DD4T.IsPreview" value="true" />

Only if this switch is present and set to true, the XPM markup will be generated in your HTML sources.

Models Ready for XPM

In order for XPM utility methods provided by DD4T out of the box to work, the models must:
  • implement interface DD4T.Core.Contracts.ViewModels.IViewModel;
  • annotate their Tridion fields with attributes;
The code below shows a simple model that is XPM ready:

using DD4T.ContentModel;
using DD4T.Core.Contracts.ViewModels;
using DD4T.Mvc.ViewModels.Attributes;
using DD4T.ViewModels.Attributes;
using System.Web.Mvc;

public class Article : IViewModel
{
    [TextField(FieldName = "title")]
    public string Title { get; set; }

    [RichTextField(FieldName = "summary")]
    public MvcHtmlString Summary { get; set; }

    public IModel ModelData { get; set; }
}

Notice the attributes TextField and RichTextField mapping the Tridion XML names to the model properties.

Also, the model declares property ModelData of type IModel inherited from interface IViewModel. It must be assigned either a DD4T.ContentModel.IComponentPresentation or DD4T.ContentModel.IPage object.

XPM Code for the Views

The following code will enable XPM Javascript output in your Razor Views. Several Page View and Component Views must be edited to enable XPM markup.

Page View

The following code snippet must go in a Page View at the end of the HTML document, just above the ending of the </body> tag.

@using DD4T.Mvc.ViewModels.XPM

if (XpmExtensions.XpmMarkupService.IsSiteEditEnabled())
{
    @Html.Raw(
        XpmExtensions.XpmMarkupService.RenderXpmMarkupForPage(
            Model, "http://tridion-cme.com"
        )
    );
}

You can notice the if statement surrounding the XpmExtensions output. This is required at the moment of writing this post because the method RenderXpmMarkupForPage does not check the enable status of XPM. (This should change in a subsequent DD4T release).

The code above should generate the following HTML output (or similar):

<!-- Page Settings: {"PageID":"tcm:9-259-64","PageModified":"2016-08-04T14:43:31","PageTemplateID":"tcm:9-264-128","PageTemplateModified":"2016-10-18T12:37:03"} -->
<script type="text/javascript" language="javascript" defer="defer" src="http://tridion-cme.com/WebUI/Editors/SiteEdit/Views/Bootstrap/Bootstrap.aspx?mode=js" id="tridion.siteedit"></script>

Component View

The following code goes in the Component Views. In order for the XPM logic to work properly when moving/delimiting Component Presentations, make sure to surround your CP output by an HTML tag (usually div).

@using DD4T.Mvc.ViewModels.XPM;

<div>
    @if (XpmExtensions.XpmMarkupService.IsSiteEditEnabled())
    {
        @Model.StartXpmEditingZone()
    }
...
</div>

The StartXpmEditingZone method is defined in class DD4T.Mvc.ViewModels.XPM.XpmExtensions and is an extension method for object based on interface DD4T.Core.Contracts.ViewModels.IViewModel. This implies your 'model' must be implementing interface IViewModel.

You can notice the if statement surrounding the StartXpmEditingZone output. This is required at the moment of writing this post because the method does not check the enable status of XPM. (This should change in a subsequent DD4T release).

The code above will generate the following HTML (or similar):

<!-- Start Component Presentation: {"ComponentID" : "tcm:9-266",
  "ComponentModified" : "2016-10-18T13:28:10", "ComponentTemplateID" : "tcm:9-264-32",
  "ComponentTemplateModified" : "2016-07-26T10:26:25", "IsRepositoryPublished" : false} -->

Text Field

The following code outputs special HTML markup that enables XPM in-line editing for a text field:

@using DD4T.Mvc.ViewModels.XPM;
@model Article

<h1>
    @Model.XpmEditableField(m => m.Title)
</h1>

Extension method XpmEditableField outputs the XPM HTML markup as well as the value of the model field.

There is another extension method, XpmMarkupFor, that only outputs the XPM HTML markup. This is perhaps better suited for outputting XHTML rich-text fields, where we can do more processing on the rich-text value before outputting it.

The code above will generate the following HTML:

<h1>
  <!-- Start Component Field: {"XPath":"tcm:Content/custom:Content/custom:title"} -->About Us
</h1>

Embedded Field

Outputting XPM markup for in-line editable embedded fields, single or multi-valued, is a bit trickier. The code looks similar to the following snippet:

@using DD4T.Mvc.ViewModels.XPM;
@model Article

@foreach (EmbeddedParagraph paragraph in Model.Paragraphs)
{
    <span>
        @paragraph.XpmMarkupFor(m => m.Text)
        @Html.Raw(paragraph.Text)
    </span>
}

Notice the construct on the embedded paragraph of type EmbeddedParagraph. We use the XPM extension method XpmMarkupFor on the paragraph model itself.

The code above generates the following HTML output:

<span>
    <!-- Start Component Field: {"XPath":"tcm:Content/custom:Content/custom:paragraph[1]/custom:text"} -->
    Text <b>goes</b> here
</span>

Conclusion

At this moment, if you followed the steps so far, you will have a working XPM implementation without the need of republishing anything. One thing that at this moment still doesn't work is Session Preview.

In other words, what will for at this moment:
  • Enable/disable XPM functionality on a Page in your website;
  • Move/Reorder Component Presentations on the Page;
  • Add existing Component Presentation on the Page;
  • Create new content and add it to the Page;
For short, all editorial functionality of XPM will be there.

What will not work at the moment is Fast Track Publishing, also known as Session Preview, also known as Update Preview. But, more about that, in the next post Enable XPM Session Preview in Web 8 using Microservices.



Comments

Popular posts from this blog

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: https://dba.stackexchange.com/questions/145982/sp-updatestats-vs-update-statistics I stumbled upon the following post from 2008 (!!!), https://social.msdn.microsoft.com/Forums/sqlserver/en-US/186e3db0-fe37-4c31-b017-8e7c24d19697/spupdatestats-fails-to-run-with-permission-error-under-dbopriveleged-user , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a

Debugging a Tridion 2011 Event System

OK, so you wrote your Tridion Event System. Now it's time to debug it. I know this is a hypothetical situtation -- your code never needs any kind of debugging ;) but indulge me... Recently, Alvin Reyes ( @nivlong ) blogged about being difficult to know how exactly to debug a Tridion Event System. More exactly, the question was " What process do I attach to for debugging even system code? ". Unfortunately, there is no simple or generic answer for it. Different events are fired by different Tridion CM modules. These modules run as different programs (or services) or run inside other programs (e.g. IIS). This means that you will need to monitor (or debug) different processes, based on which events your code handles. So the usual suspects are: dllhost.exe (or dllhost3g.exe ) - running as the MTSUser is the SDL Tridion Content Manager COM+ application and it fires events on generic TOM objects (e.g. events based on Tridion.ContentManager.Extensibility.Events.CrudEven