I am using HtmlString to render my images by default so I can edit the image in the expierence editor.

I am using the fieldrenderer

model.AllgemeinSaxoprintLogo = new HtmlString(FieldRenderer.Render(item, Templates.HeaderKomplex.Fields.Logo.ToString()));

but by default the fieldrenderer uses the original file name and not the display name.

Is it possible to configure it so that the display name is used and if just a simple configure is not working perhaps a hint for a possible solution would be nice. Because I've tried this MediaUrlOptions and here I can't find any option to use the display name.

Example default url for image with name "newsletter-bg-header.png":


when I set the following display name for my Image "nlbg-header" it would be nice if I could get this output


My Solution

is depending on the accepted answer. And I had to create a customMediaProvider like descriped. But I only need to overwrite the GetMediaUrl function the other stuff like GetMediaData was working out of the box don't need to overwrite this. So I've decompiled the MediaProvider and used the hole existing function and just pimped 4 line of code

   public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
        Assert.ArgumentNotNull((object)item, nameof(item));
        Assert.ArgumentNotNull((object)options, nameof(options));
        Assert.IsTrue(this.Config.MediaPrefixes[0].Length > 0, "media prefixes are not configured properly.");

        string str1 = this.MediaLinkPrefix;

        if (options.AbsolutePath)
            str1 = options.VirtualFolder + str1;
        else if (str1.StartsWith("/", StringComparison.InvariantCulture))
            str1 = StringUtil.Mid(str1, 1);

        string part2 = MainUtil.EncodePath(str1, '/');

        if (options.AlwaysIncludeServerUrl)
            part2 = FileUtil.MakePath(string.IsNullOrEmpty(options.MediaLinkServerUrl) ? WebUtil.GetServerUrl() : options.MediaLinkServerUrl, part2, '/');

        string str2 = StringUtil.EnsurePrefix('.', StringUtil.GetString(options.RequestExtension, item.Extension, "ashx"));
        string str3 = options.ToString();

        if (options.AlwaysAppendRevision)
            string str4 = Guid.Parse(item.InnerItem.Statistics.Revision).ToString("N");
            str3 = string.IsNullOrEmpty(str3) ? "rev=" + str4 : str3 + "&rev=" + str4;

        if (str3.Length > 0)
            str2 = $"{str2}?{str3}";
        string str5 = "/sitecore/media library/";

        //That the original code where the path and the original file name are returned   
        string path = item.InnerItem.Paths.Path;

        //Jsut this lines of code was enough to get my Display name as file name
        if (!string.IsNullOrEmpty(item.DisplayName) && path.EndsWith(item.Name))
            var pathWithoutFilename = path.Remove(path.Length-item.Name.Length);
            path = $"{pathWithoutFilename}{item.DisplayName}";
        //PIMPING END

        string str6 = MainUtil.EncodePath(!options.UseItemPath || !path.StartsWith(str5, StringComparison.OrdinalIgnoreCase) ? item.ID.ToShortID().ToString() : StringUtil.Mid(path, str5.Length), '/');
        string str7 = part2 + str6 + (options.IncludeExtension ? str2 : string.Empty);
        if (!options.LowercaseUrls)
            return str7;
        return str7.ToLowerInvariant();

here you could also pimp it more so that you could use the displayname for the hole image path, but I am not shure how to get the parent items "fast" enough.

  • Are you talking about the image url generated is based on the original file name instead of display name? Commented Mar 27, 2019 at 2:17
  • @FredricFoo Yes Commented Mar 27, 2019 at 6:05

1 Answer 1


Media url is generated based on Media Item Path, not the original file name, as you mentioned.

But MediaUrlOptions uses property IncludeExtension to add or remove extension.

In your case, just set IncludeExtension to false and you will get url like /-/media/.../[MEDIA_ITEM_NAME].

Note. Solution was found after decompiling Sitecore.Resources.Media.MediaProvider class, method GetMediaUrl(MediaItem item, MediaUrlOptions options). Here is this part of code:

// here we get media item path
string path = item.InnerItem.Paths.Path;
// UseItemPath - should we use path or ID of item
string str6 = MainUtil.EncodePath(!options.UseItemPath || !path.StartsWith(str5, StringComparison.OrdinalIgnoreCase) ? item.ID.ToShortID().ToString() : StringUtil.Mid(path, str5.Length), '/');
// check to include extension in url or not - JUST SET IncludeExtension to False
string str7 = part2 + str6 + (options.IncludeExtension ? str2 : string.Empty);

I think there is no Sitecore implementation, that uses DisplayName instead of Sitecore item path. If you really (think twice first) need it you need to define your own MediaProvider.

Here is what I mean:

class CustomMediaProvider : MediaProvider
    public virtual string GetMediaUrl(MediaItem item, MediaUrlOptions options)
      // generate you url here mannually

    protected override MediaData GetMediaData(MediaUri mediaUri)
        // manually retrieve item by it's mediaUri.path

Then you could register your own implementation:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
                   <patch:attribute name="type">MyProject.Providers.CustomMediaProvider, MyProject</patch:attribute>

Simplified default implementation:

protected override MediaData GetMediaData(MediaUri mediaUri)
    string mediaPath = mediaUri.MediaPath;
    Item obj = database.GetItem(mediaPath);
    return MediaManager.Config.ConstructMediaDataInstance((MediaItem) obj);

Here Sitecore retrieves item by it's path and converts it to media. It'll be very difficult to fetch item by it's DisplayName.

  • Finally, updated answer, as explanation is really long. Commented Mar 27, 2019 at 19:20
  • I think to get the item by its displayname works out of the box, because I've added the displayname to an image and renamed the imagename inside my rendered html and here the image was still rendered. Its currently only not possible to render the display name out of the box, so I think I only need to implement the GetMediaUrl function. Do I have to register this Custom Provider somewhere? Commented Mar 27, 2019 at 21:48
  • Answer updated with config patch. Commented Mar 28, 2019 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.