Here's how you would go about getting your file URL:
//mediaId is the ID returned from your document property...
Media file = new Media(mediaId);
string url = file.getProperty("umbracoFile").Value.ToString();
I appreciate all the input. I need to go with the C# approach for this requirement because it's part of a user control (I could probably use some in-line XSLT for that).
Nik - your solution works like a charm - exactly what I was looking for. Thanks.
So it is working. However, there are a couple of things I do not like about this. Feels like a hack. More importantly the Media object is marked as depricated.
If there is a better more up to date way to do this I would appreciate it.
It sounds like you're using the old APIs - in v6, new (cleaner, better, shinier) APIs were introduced.
There doesn't seem to be much documentation on the MediaService at the moment, but I imagine it's similar to ContentService and should be self explanatory if you look at the methods in autocomplete. There is some documentation on the new Media object that you'll get back when using the MediaService, though.
The only thing that I might want to tinker with is that you have to start from a known Media Folder Id. I assume it is possible to create multiple media folders and if that is the case this code would not find all the media. It might be desireable to be able find media regardless of what folder it is in.
Are you wanting to search all media or only under a certain folder? You might have a play with GetRootMedia, which I think returns all items at the root level, then check their descendants. Or if you're only looking for a certain media type, maybe GetMediaOfMediaType?
var mediaFiles = ApplicationContext.Current.Services.MediaService.GetRootMedia();
You get nothing back. Not sure why.
My current solution should work as we were planning on having them use one folder in particular for this functionality. I was just trying to be proactive as I am sure someone will one day create a new folder and then wonder where their content is.
I also think it is probably more likely that we will want to add content that is not returned in the search mechanism I am putting together. If that is the case the current solution will be best.
Hmm, strange. Maybe a bug worth reporting on the issue tracker.
Last suggestion, you could add a media picker somewhere on your site to select which folder to use for this script, then pull in that ID dynamically. That way it doesn't require a code change when the folder changes later.
Perhaps what you're looking for then is the "GetMediaOfMediaType(int id)" method of the Umbraco v6 API MediaService. The `id` parameter is the ID of the MediaType you would like to retrieve/use.
Get URL of media file
Hello Umbraco Colleagues,
How can I retreive the URL of files stored in the media section of Umbracon 4.0.2.1 in C#?
The property in the doc type for these files is a Media Picker.
I need to show a list of hyperlinks to PDFs that are stored as media files in Umbraco. When the user clicks on a link, the PDF will load.
Should be simple, but I haven't figured it out yet. Any help would be greatly appreciated!
Thanks,
David Hill
Hi David
I think you can use the umbraco.library:GetMedia function in C# even though I just normally use it in XSLT instead.
If you just need to make a list of PDF's I think I would go with XSLT.
There is a good example of this in the WIKI section about the umbraco.library, which you can see here http://our.umbraco.org/wiki/reference/umbracolibrary/getmedia
Assuming that you have a MediaPicker property set on your node referencing a folder in the media library the XSLT could look something like this
<ul>
<xsl:for-each select="umbraco.library:GetMedia($currentPage/data [@alias = 'pdfFolder'], 'false')/descendant::node">
<li>
<a href="{umbraco.library:GetMedia(@id, 'false')/data [@alias = 'umbracoFile']}"><xsl:value-of select="@nodeName" /></a>
</li>
</xsl:for-each>
</ul>
Hope this is usefull.
/Jan
Just discovered this WIKI-entry by Warren, which may be helpfull to you if you choose to go with the XSLT approach: http://our.umbraco.org/wiki/reference/code-snippets/listfilesfrommediafolderxslt
Hi David,
Here's how you would go about getting your file URL:
HTH,
Nik
Thanks everyone.
I appreciate all the input. I need to go with the C# approach for this requirement because it's part of a user control (I could probably use some in-line XSLT for that).
Nik - your solution works like a charm - exactly what I was looking for. Thanks.
David
Perfect, glad that worked out. Yeah, sometimes XSLT just won't do :) Don't get me wrong, I love it when I can use it...
Nik - maybe you could write a simple sample in the WIKI on how to get meida files with C#...that would be cool :-)
/Jan
Sure, I'd be happy to...i'll add it shortly.
-- Nik
I am in umbraco v6.+
I am looking for a way to build a list of all media added to the media content.
After poking around I came up with:
public void GetMediaFileList()
{
int[] ids = Media.getAllUniqueNodeIdsFromObjectType(new Guid("B796F64C-1F99-4FFB-B886-4BF4BC011A9C"));
List<string> fileNames = new List<string>();
string url = String.Empty;
foreach (int mediaId in ids)
{
Media file = new Media(mediaId);
if (file.ParentId > 0)
{
//url = file.getProperty("fileName").Value.ToString();
url = file.getProperty("umbracoFile").Value.ToString();
int pos = url.LastIndexOf(@"/");
fileNames.Add(url.Substring(pos + 1));
}
}
sendJSON(new { files = fileNames });
}
The out put from this is (I only have three items in the media folder at this point):
So it is working. However, there are a couple of things I do not like about this. Feels like a hack. More importantly the Media object is marked as depricated.
If there is a better more up to date way to do this I would appreciate it.
Thanks.
Hi Geoffrey,
It sounds like you're using the old APIs - in v6, new (cleaner, better, shinier) APIs were introduced.
There doesn't seem to be much documentation on the MediaService at the moment, but I imagine it's similar to ContentService and should be self explanatory if you look at the methods in autocomplete. There is some documentation on the new Media object that you'll get back when using the MediaService, though.
Hope this helps get you started!
-Tom
Thanks Tom. Huge improvement.
Finding the right documentation has been a bit tricky.
This code does the same thing as the code above:
public void GetMediaFileList()
{
List fileNames = new List();
var mediaFiles = ApplicationContext.Current.Services.MediaService.GetDescendants(1198);
string url = String.Empty;
foreach (Media file in mediaFiles)
{
url = file.GetValue("umbracoFile").ToString();
fileNames.Add(Path.GetFileName(url));
}
sendJSON(new { file = fileNames });
}
The only thing that I might want to tinker with is that you have to start from a known Media Folder Id. I assume it is possible to create multiple media folders and if that is the case this code would not find all the media. It might be desireable to be able find media regardless of what folder it is in.
Very nice! Glad that helped.
Are you wanting to search all media or only under a certain folder? You might have a play with GetRootMedia, which I think returns all items at the root level, then check their descendants. Or if you're only looking for a certain media type, maybe GetMediaOfMediaType?
If you change this one line:
var mediaFiles = ApplicationContext.Current.Services.MediaService.GetRootMedia();
You get nothing back. Not sure why.
My current solution should work as we were planning on having them use one folder in particular for this functionality. I was just trying to be proactive as I am sure someone will one day create a new folder and then wonder where their content is.
I also think it is probably more likely that we will want to add content that is not returned in the search mechanism I am putting together. If that is the case the current solution will be best.
Thanks again.
Hmm, strange. Maybe a bug worth reporting on the issue tracker.
Last suggestion, you could add a media picker somewhere on your site to select which folder to use for this script, then pull in that ID dynamically. That way it doesn't require a code change when the folder changes later.
Interesting idea about the media picker. As the site grows I will keep it in mind.
New to umbraco so I am unfamiliar with the issue tracker. I will take a look for it.
http://issues.umbraco.org/issues :)
Hi Geoffrey,
Perhaps what you're looking for then is the "GetMediaOfMediaType(int id)" method of the Umbraco v6 API MediaService. The `id` parameter is the ID of the MediaType you would like to retrieve/use.
Check out the documentation on the MediaService, I've recently added all methods to the MediaService page;
http://our.umbraco.org/documentation/reference/management-v6/services/MediaService
--Steven
Hi Guys,
It looks like this has changed slightly in the new versions of the URL. It is no longer getProperty but is now GetValue("umbracoFile")....
Hope this helps.
Kind Regards
David
is working on a reply...