Media Selection - Get Media Absolute URL to work within my webpart

Alonso Gonzalez asked on October 23, 2017 17:31

Hi there,

I have a couple of questions I came with while developing a custom web part with a aspx template ( I created this web part from web parts app with a standard type and I'm including it in my "/test" page tab as a widget )

The function of this web part is to query specific page types from a given path attribute ( path selector property ) and display the items as a table with the images.

I was able to display some of the fields of my page type using a foreach cycle inside my web part layout ( .ascx ), I was able to get them showing by doing this :


(I will like to know what’s the “=“ sign for ? This code won’t work if I don’t include it. )

This code worked, all good until I tried to get the URL of a field my page type has called Media that uses a Media Selection form control

I get the value of my Media field from my code behind like :

string MediaID = doc.GetStringValue("Image", "")

Also tried with ( and got the same value back ) :

ValidationHelper.GetString(doc.GetValue("Media"), "")

For example one of my page types return : “~/test/MediaFolder/tempimage.png?width=1104&height=729&ext=.png" (visible while debugging because if I try to display in the front as : <%=row.Media%> it won’t show )

Basically I want to know how can I get the absolute URL for the media file Im uploading or selecting from the media selection form control?

When I use the “Image Selection” form control I do like :

URLHelper.GetAbsoluteUrl(TransformationHelper.HelperObject.GetDocumentUrl(doc.GetStringValue(“Media”, ""), “”));

And Im able to get the right URL to display my image, but I can’t use only “Image Selection” for this scenario because I need the user to also be able to select/upload videos.


Correct Answer

Matt Nield answered on October 23, 2017 22:17

Hi Alonso, The url stored in the media selector should be the right URL to view the images from the media library in your browser. I tried replicating your issue by adding a new field to the CMS.News document type and then, in the News detail transformation, adding a my image in using the folowing:

<img src="<%# Eval("NewsMedia") %>" title="media">

I'm using <%# rather than <%= as I'm looking at the bound item in the repeater. <%= in ASP.NET is the equavalent on Response.Write, allowing you to output of the following statement to the response stream.

If you're trying to resolve to an absolute URL, then you can make use of the methods in the URL helper class.

As you're getting that value “~/test/MediaFolder/tempimage.png?width=1104&height=729&ext=.png", you can try viewing that in your browser by removing the ~ from the from and sticking it on to your site's URL (i.e. or http://localhost/Kentico10/test/MediaFolder/tempimage.png?width=1104&height=729&ext=.png. If you can see the image, then you should be able to use it as an image source or video file etc. depending on your usecase.

1 votesVote for this answer Unmark Correct answer

Recent Answers

Alonso Gonzalez answered on October 25, 2017 06:47 (last edited on October 25, 2017 06:48)

Hi Matt,

Thanks a lot for your answer, you have helped me a lot. Sadly if I do as you suggested and remove ~ out of the “~/test/MediaFolder/tempimage.png?width=1104&height=729&ext=.png" url and try accesing it using the browser I will get :

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
0 votesVote for this answer Mark as a Correct answer

Matt Nield answered on October 25, 2017 12:24 (last edited on October 25, 2017 12:25)

Hi Alonso, I've just gone in and recreated the scenario in Kentico 10. When I look at the value stored by a Media seletor form control, I see something like this: ~/Blank/media/sample-images/Apples.jpg?ext=.jpg, which for me I can browse without the ~ character. It's a little odd that you cannot, I wonder if you have any security permission issues?

I'm only concerning myself with the current document in my web part, so I have a public property:

public string NewsMediaImage => CurrentDocument.GetValue("NewsMedia").ToString();

And then in my markup, I'm just using:

<img src="<%=NewsMediaImage %>" />

That gives me the relative URL, to get the absolute, I can do something like this to my property:

public string NewsMediaImage => URLHelper.GetAbsoluteUrl(URLHelper.ResolveUrl(CurrentDocument.GetValue("NewsMedia").ToString()));

so that I get a URL more like this:


So in summary, try out CMS.Helpers.URLHelper to resolve to the absolute.

0 votesVote for this answer Mark as a Correct answer

Alonso Gonzalez answered on October 25, 2017 17:10

Matt thanks again,

Is there any type of configuration I need to achieve getting an url as the last one you shared ?


After trying what you suggested I get the exact same thing as what you suggested in the first answer :


It's not transforming to this nice getmedia url

0 votesVote for this answer Mark as a Correct answer

Matt Nield answered on October 25, 2017 17:24 (last edited on October 25, 2017 17:26)

The difference in the URL style are down to a setting in Kentico:

  1. Press <F2> top open the application menu adn select the Settings application
  2. Select Content > Media
  3. In the General tab, there is a setting called Use permanent URLs. If set to true, URLs of the media library files will be generated as permanent links using getmediafile.aspx

For it to make a difference howver, you would need to re-select the images using the Media selector control adn save the value. This is because - in this instance - Kentico stores the path, not the ID of the media file.

0 votesVote for this answer Mark as a Correct answer

Alonso Gonzalez answered on October 25, 2017 17:57


I have changed the configuration and set it to true, but still in my code the url looks as before, same in field:

Image Text

Any idea what I may be doing wrong ?

0 votesVote for this answer Mark as a Correct answer

Matt Nield answered on October 25, 2017 18:19

Hi, yep - I sure do. I didn't until I saw your screenshot though. I've been working though all of my samples using the Media library, rather than Content, which is what you have here. I don't think that the File page type is ever serverd through getmedia.aspx. That's my misunderstanding, sorry.

Either way though, the URL you're seeing should be visible to users so long as the security is set up correctly, it is just content in the tree after all. Are you still haveing trouble serving the images, or is it just that you now want the URL to be more 'freindly'?

0 votesVote for this answer Mark as a Correct answer

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