How to leverage the Title and Property of a Media Library image from a PageType

Pierre Clément asked on May 11, 2023 15:15

Hello, I'm currently using Kentico 13.0.70 and I've created a page type with a text field and the edit control is a "Media Selector". When I create a page with this page type, I am able to select an image from the media library. The Image in the Media Library has a Title and a description.

When I save the selection, the data is the relative url of the image which is fine at some point because we can simply create an tag with that data and the image is shown.

The problem is I want to include the Title and the Description into the tag but since I only have it's url, I am not able to retrieve the MediaFileItem from the database and retrieve the fields from there.

I've looked at the DancingGoat demo site and it never fetches the data from the MediaFile but always put the title and alttext from the page instead.

P.S. If I have to update Kentico to a newer version, I can do this.

Regards, Pierre

Correct Answer

Arjan van Hugten answered on May 11, 2023 16:40

We created an 'ExtractGuid' helper method to get the GUID value for the media file out of the URL. The GUID is used with the 'IMediaFileInfoProvider' to retrieve the 'MediaFileInfo' in which you can find the 'FileTitle' and 'FileDescription'.

public static Guid ExtractGuid(this string src)
{
    var guidStringValue = Regex.Match(src,
        @"[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}")
        .Value;

    return Guid.TryParse(guidStringValue, out var guid) ? guid : Guid.Empty;
}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Pierre Clément answered on May 19, 2023 14:35

Hello Arjan van Hugten,

With this solution, we have to set the file url to use the pernament link and not just the relative link, am I right? In this case, we'll have to use the Regex to get the GUID from it.

Regards, Pierre

0 votesVote for this answer Mark as a Correct answer

fukkilurdu fukkilurdu answered on May 19, 2023 17:55

I think it definitely works by setting the file URL to use a permanent link

0 votesVote for this answer Mark as a Correct answer

Pierre Clément answered on May 19, 2023 21:35

Shouldn't it be included by default in the MediaProvider? It doesn't make sense to have to do this hack instead of having it available through the provider since most of people will use the MediaLibrary to reuse images / files with it's metadata instead of having to upload the same image / file over and over again and when you have to change the metadata, you'll have to go through all pages and change it.

I mean, when I use a File as the Field Type, I have the amazing DocumentAttachment object that I can use in the code. Why can't we have a "Media Library File" Field Type that it'll simply map to a C# object and have the metadata from it?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.