World is now on Opti ID! Learn more

Jacob Khan
Apr 5, 2016
  3082
(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
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |