Adding a Processor to getMediaUrlOptions Pipeline

Recently, I had to enable CDN for our Sitecore instances. After following the instructions available in Sitecore documentation, everything seemed to work fine.

There was use case where we had to exclude PDF documents from the CDN route to keep the security for some of the documents in place. The suggested approach was to add a processor in the getMediaUrlOptions pipeline.

So I create a below processor:

using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.GetMediaUrlOptions;
using Sitecore.Pipelines.GetMediaUrlOptions.Processors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Foundation.Core.Pipelines.MediaLinks
{
    public class MediaLinksProcessor : GetMediaUrlOptionsProcessor
    {
        public override void Process(GetMediaUrlOptionsArgs args)
        {
            Assert.ArgumentNotNull((object)args, nameof(args));

            if (!args.MediaItem.Extension.ToLowerInvariant().Contains("pdf"))
            {
                return;
            }

            args.MediaUrlBuilderOptions.AlwaysIncludeServerUrl = false;
        }
    }
} 

And added a config file to patch the processor:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
	<sitecore>
		<pipelines>
			<getMediaUrlOptions>
				<processor type="Foundation.Core.Pipelines.MediaLinks.MediaLinksProcessor, Foundation.Core" patch:after="processor[@type='Sitecore.Pipelines.GetMediaUrlOptions.Processors.SiteModeProcessor, Sitecore.Kernel']" />
			</getMediaUrlOptions>
		</pipelines>
	</sitecore>
</configuration>

After adding this processor, the pdf files were excluded from the CDN Urls and we got the expected results.

The reference I liked is listed below:
https://maciejz.dev/cdn-url-in-sitecore-shell/

Hope this helps for someone looking for the similar use case.

Thank you.. Keep Learning.. Keep Sitecoring.. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s