Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get media url including server part

Tags:

c#

sitecore

Is it possible to get url with MediaManager.GetMediaUrl that always includes the server part?

like image 715
DixonD Avatar asked May 25 '11 10:05

DixonD


People also ask

How do I find the media item URL in Sitecore?

Getting the URL of a MediaItem is pretty straightforward, you pass the MediaItem reference to Sitecore. Resources. Media. MediaManager.

What is the default URL for an item in Sitecore?

The default is filePath . Specifies if Sitecore lowercases the URLs it generates.


2 Answers

Just to bump this up, in Sitecore 7 the AlwaysIncludeServerUrl option is also included in MediaUrlOptions (I don't know since which version of Sitecore)

Like this:

MediaUrlOptions muo = new MediaUrlOptions();
muo.AlwaysIncludeServerUrl = true;
String url = MediaManager.GetMediaUrl((MediaItem)item, muo);
like image 136
DdW Avatar answered Oct 22 '22 21:10

DdW


I've discovered that the following will work for producing fully qualified urls for media items:

public static string GetMediaUrlWithServer(MediaItem mediaItem, Item item = null)
{
    item = item ?? Sitecore.Context.Item;
    var options = new UrlOptions {AlwaysIncludeServerUrl = true, AddAspxExtension = false};
    var itemUrl = LinkManager.GetItemUrl(item, options);
    var mediaOptions = new MediaUrlOptions {AbsolutePath = true};
    var mediaUrl = MediaManager.GetMediaUrl(mediaItem, mediaOptions);
    return itemUrl + mediaUrl;
}

The urls produced will be relative to item so you may want to supply a reference to your Home item instead of Sitecore.Context.Item

like image 27
zzzzBov Avatar answered Oct 22 '22 20:10

zzzzBov