Publishing Media items throws error when no media is attached

Kamruz Jaman - Partner

16 Mar 2022

Share on social media

A few months ago, we had been working on a very large migration project to Sitecore 10.1. Very close to launch we were running some final checks and after syncing content from the existing production instance we started to receive errors when trying to publish items.

The following error was being thrown and the publish would not complete successfully:

Job started: Publish to 'web'|#Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Sitecore.Framework.Data.Blobs.Abstractions.BlobProviderException: No supported provider for is configured. at Sitecore.Framework.Data.Blobs.BlobStorage.GetBlobProvider[T](BlobIdentifier identifier) at Sitecore.Publishing.PublishHelper.CopyBlobField(Field sourceField, Item targetVersion) at Sitecore.Publishing.PublishHelper.CopyBlobFields(Item sourceVersion, Item targetVersion) at Sitecore.Publishing.PublishHelper.TransformToTargetVersion(Item sourceVersion) at Sitecore.Publishing.PublishHelper.CopyToTarget(Item sourceVersion, Item originalItem) at Sitecore.Publishing.PublishHelper.PublishVersionToTarget(Item sourceVersion, Item targetItem, Boolean targetCreated) at Sitecore.Publishing.PublishHelper.PublishVersion(Item sourceVersion) at Sitecore.Publishing.Pipelines.PublishItem.PerformAction.Process(PublishItemContext context)

The error seemed like it was related to the items in the media library, since the complaint about was Blobs. This particular project had well over 300k items in the media library alone, so pinpointing the issue took a little bit of time but I was able to replicate this locally on a clean Sitecore 10.1 instance.

To replicate this on a clean instance of Sitecore (in Docker of course!):

  1. Upload a new image to the media library
  2. Publish the item
  3. Confirm success
  4. From the Media field, Detach the item, and save.
  5. Try to publish the item again, note the exception

Rather annoyingly of course the error message does not provide any clues as to which item the error is due to, which does not help the Content Authors to figure out which item to fix.

I also checked this same scenario on a Sitecore 9.3 instance, and that published without any errors. My guess is that this error was introduced in Sitecore 10.1 when the Dataprovider was refactored for the Items As Resource (IAR) files, but that's just a hunch.

So of course I raised a ticket with Sitecore Support, who confirmed the bug and provided a workaround. Run the following SQL against your master database, you only need to run the 2nd one if you are using Versioned media although it should not do any harm to run it anyway.

SELECT ItemId FROM [dbo].[SharedFields] where FieldId='{40E50ED9-BA07-4702-992E-A912738D32DC}' and [Value] = '' or [Value] is null

SELECT ItemId FROM [dbo].[Fields] where FieldId='{DBBE7D99-1388-4357-BB34-AD71EDF18ED3}' and [Value] = '' or [Value] is null

You can then delete or fix items which are returned. Use the search from the Content Tree or from the Search box in the bottom right of the Sitecore Desktop to quickly locate the items.

And whilst this fixes the immediate issue, it was really only a temporary fix and something that was likely to occur very quickly again - I already mentioned the 300k in the Media Library, well the Content Tree also had close to 1 million items, and several 10s of Content Authors around the globe. We didn't want to just tell them to not do this and hope they all remembered or any new authers were also told. Last thing we needed was a publish failure half way through our night, and then someone having to jump in to run a SQL statement to figure out which item was causing the error. If the error message had at least provide an item id then it would have been a little less of an issue, even if it was not ideal.

So back to Sitecore Support but unfortunately this was the best they could suggest due to "limitations".

The solution to prevent this was actually very trivial. We can just tap into the publish pipeline ourselves and check if the Media Item has a Blob stream, and if it does not then we can set the PublishAction to DeleteTargetItem or Skip (as required). Let's log this as a warning too so we can identify and fix these items later.

using Sitecore; using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.Publishing; using Sitecore.Publishing.Pipelines.PublishItem; namespace Konabos.Publishing.Pipelines.Publish { // Resolves bug that throws error when publishing items without any media attached // Can be removed if it is fixed by Sitecore in a future release public class ConfirmMediaItemHasBlob : PublishItemProcessor { public override void Process(PublishItemContext context) { Assert.ArgumentNotNull((object) context, nameof(context)); if (context.Action != PublishAction.PublishSharedFields && context.Action != PublishAction.PublishVersion) return; Item item = context.PublishHelper.GetItemToPublish(context.ItemId); if (item != null && item.Paths.IsMediaItem && item.TemplateID != TemplateIDs.MediaFolder) { MediaItem media = new MediaItem(item); if (!media.FileBased && !media.HasMediaStream("Blob")) { context.Action = PublishAction.DeleteTargetItem; //or Skip depending on your needs Log.Warn($"Media item has no valid stream attached. Item deleted instead. [{item.Paths.FullPath}]", this); } } } } }

And let's patch this in:

<pipelines> <publishItem> <processor type="Konabos.Pipelines.Publish.ConfirmMediaItemHasBlob, Konabos.Publishing" patch:after="*[@type='Sitecore.Publishing.Pipelines.PublishItem.DetermineAction, Sitecore.Kernel']" /> </publishItem> </pipelines>

This is registered as a bug 490149 with Sitecore and suspect it will be resolved in the next release. In the meantime, no publish failures and happy Content Authors!

Sign up to our newsletter


Sitecore Community
Sitecore XP

Share on social media

Kamruz Jaman

Kamruz is a 10-time Sitecore MVP who has worked with the Sitecore platform for more than a decade and has over 20 years of development and architecture experience using the Microsoft technology stack. Kamruz is heavily involved in the Sitecore Community and has spoken at various User Groups and Conferences. As one of the managing partners of Konabos Consulting, Kamruz will work closely with clients to ensure projects are successfully delivered to a high standard.

Subscribe to newsletter