Take the community feedback survey now.

Jacob Khan
Apr 5, 2016
  3094
(0 votes)

Preview Content in View Mode

One of our enterprise customers recently approached me with an interesting challenge. They wanted to generate a URL of unpublished content and send it to an external party for validation of look and feel and an obligatory “thumbs up” before publishing.

In CMS 6 this could be achieved by going to the template URL and adding the version id. www.alloy.com/templates/pages/standardpage.aspx?id=5_5

However editors wouldn’t know the template / controller in newer version of Episerver, so I needed a different way of accomplishing this.

I used partial routing to fetch the previous version and show that in the URL.

 public class PreviewPartialRouter : IPartialRouter<SitePageData, PageVersion>
    {

        public PartialRouteData GetPartialVirtualPath(PageVersion version, string language, RouteValueDictionary routeValues, RequestContext requestContext)
        {
            var contentLink = ContentRoute.GetValue("node", requestContext, routeValues) as ContentReference;
         
            if (PageEditing.PageIsInEditMode)
            {
                return null;
            }

            return new PartialRouteData
            {
                BasePathRoot = contentLink,
                PartialVirtualPath = String.Format("{0}/", version.ToString())
            };

        }


        public object RoutePartial(SitePageData content, EPiServer.Web.Routing.Segments.SegmentContext segmentContext)
        {
            var namePart = segmentContext.GetNextValue(segmentContext.RemainingPath);
            if (!String.IsNullOrEmpty(namePart.Next))
            {
                var version = HttpUtility.UrlDecode(namePart.Next);
                try
                {
                    var versionNumber = new ContentReference(version);

                    if (versionNumber != null)
                    {
                        var remaingPath = namePart.Remaining;
                        //Update RemainingPath on context.
                        segmentContext.RemainingPath = remaingPath;
                        var versionRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentVersionRepository>();

                        var ActualVersion = versionRepository.Load(versionNumber);

                        segmentContext.RoutedContentLink = ActualVersion.ContentLink;
                        var contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentLoader>();
                        var pd = contentRepository.Get<SitePageData>(ActualVersion.ContentLink);
                        segmentContext.SetCustomRouteData<PageVersion>("version", (PageVersion)ActualVersion);


                        return pd;
                    }        
                 }catch{
                    return content;
                 }

              

                       
            }

            return null;
        }	

This example is with Alloy, where I use sitepagedata as my base. As you can see, I am getting a request in for a partial route meaning the main page url /”something”. “Something” refers to ContentReference.ID + “_” + Version Workid (5_18). This enables me to switch the current version to an older one.

I also need to register my partial route:

 [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class PreviewInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
          
            var partialRouter = new PreviewPartialRouter();

            RouteTable.Routes.RegisterPartialRouter<SitePageData, PageVersion>(partialRouter);

        }

      
        public void Preload(string[] parameters) { }

        public void Uninitialize(InitializationEngine context)
        {
           
    
        }
    }

Rather simple if you ask me, but works well. To get the version ID, go into the version gadget and pick the previous version you would like to send. Since it isn’t always super easy to find a version and work id, I created a gadget which creates a simple version link for you

PreviewContent

You can download the code here. Just place it in your project under the Business folder if you are using Alloy. In your own project, make sure to fix the path for the PreviewComponent and any namespace or SitePageData issues to your base

https://drive.google.com/open?id=0B_XVbM6ery9FcXEzbkZ4Skt3YUE
Apr 05, 2016

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP - Opticon London 2025

This installment of a day in the life of an Optimizely OMVP gives an in-depth coverage of my trip down to London to attend Opticon London 2025 held...

Graham Carr | Oct 2, 2025

Optimizely Web Experimentation Using Real-Time Segments: A Step-by-Step Guide

  Introduction Personalization has become de facto standard for any digital channel to improve the user's engagement KPI’s.  Personalization uses...

Ratish | Oct 1, 2025 |

Trigger DXP Warmup Locally to Catch Bugs & Performance Issues Early

Here’s our documentation on warmup in DXP : 🔗 https://docs.developers.optimizely.com/digital-experience-platform/docs/warming-up-sites What I didn...

dada | Sep 29, 2025

Creating Opal Tools for Stott Robots Handler

This summer, the Netcel Development team and I took part in Optimizely’s Opal Hackathon. The challenge from Optimizely was to extend Opal’s abiliti...

Mark Stott | Sep 28, 2025

Integrating Commerce Search v3 (Vertex AI) with Optimizely Configured Commerce

Introduction This blog provides a technical guide for integrating Commerce Search v3, which leverages Google Cloud's Vertex AI Search, into an...

Vaibhav | Sep 27, 2025

A day in the life of an Optimizely MVP - Opti Graph Extensions add-on v1.0.0 released

I am pleased to announce that the official v1.0.0 of the Opti Graph Extensions add-on has now been released and is generally available. Refer to my...

Graham Carr | Sep 25, 2025