Sitecore Content Editor Warning for large images

Created: 4 Sep 2020, last update: 30 Jan 2022

Sitecore Content Editor Warning for large images

It sometimes happens that a large media is uploaded to the Sitecore media library and also unscaled is used on the website, this may be undesirable. A solution can be using Sitecore Content Hub where you can use image renditions. But besides that educate the content editor and show a warning may be a nice solution.

Education and showing a warning will help. This is pretty easy to do in Sitecore with the rule engine without C# backend code. See Prevent use of png media for a similar rule.

For creating the rule, you can use the “Size” field of a media, because a media can also be, for example, a PDF where the size rule does not have to apply. It is useful to first check which template is being used or you can also look at the file extension. For this we look at the template.

We need this rule: Where (template is image or jpeg) and size > 50000 bytes. You can not set brackets in the rule engine. But you can easily split in up to make it happen.

So first we make a predefined rule “Template is Image”, you can do that in the Marketing Control Panel or in the content editor below /sitecore/system/Marketing Control Panel/Personalization/Predefined Rules. In this rule, we check on template /System/Media/Unversioned/Image and Jpeg, there is also a versioned version is you need that you can add it. Use multiple where the item template is specific template conditions.

Create now the Media Size rule below /sitecore/system/Settings/Rules/Content Editor Warnings/Rules
And use the predefined rule (where predefined rule is true) and do a check on the Size field. You can do this with the “where the specific field compares to specific value” and use a regex.
For the regex you can use ([0-9]{2}|[56789])[0-9]{5}, means greater than (2 digits or 1 digit greater than or equal to 5) followed by 5 digits.. so >= 500000 bytes.

And it is done the rule is now active.

 

Note in the Sitecore config there is <setting name="Media.MaxSizeInDatabase" value="500MB"/> You can changes that to set a hard limit for all new uploaded media.  If you want big files, For Sitecore Content Hub the upload size limit is set to 8 GB per file.