1

I have read these posts

https://our.umbraco.org/forum/templates-partial-views-and-macros/85993-archetype-mediapicker-udi

https://our.umbraco.org/forum/using-umbraco-and-getting-started/85598-getting-image-url-from-media-picker-not-working-in-76

http://www.codeshare.co.uk/blog/how-to-get-the-file-path-of-a-media-item-in-umbraco/

but I'm still unable to get the value from the media picker.

On my last attempt, I got the following error.

Object reference not set to an instance of an object

If I try the following:

string img = "";
            foreach (var d in archtypePanel.Properties)
            {
                if (d.Alias == "imageToDisplay")
                {
                    img = d.Value.ToString();
                }
            }

I can see the media UDI: "umb://media/15cc14d318f3444a86ab3bc7fc5787ad" but I'm unable to get any further.

I have now run into a wall, so any help on solving this issue will be helpful.

My last attempt is below:

Guid idValue = new Guid(id);
            var blogPosts = CurrentPage;
            IPublishedContent blogPosts2 = CurrentPage;
            var archtypePanel = blogPosts2.GetPropertyValue<ArchetypeModel>("blogArchetypes").First(x=>x.Id == idValue);

            ArchetypeWithTextAndImageModel model = new ArchetypeWithTextAndImageModel
            {
                TextContent = new HtmlString(archtypePanel.GetValue("textRichTextEditor")),
                ImageId     = archtypePanel.GetValue<IEnumerable<IPublishedContent>>("imageToDisplay").ToString()// imgUrl
            };

But ImageId throws the error.

3 Answers 3

4

Access from strong type model

make sure your Umbraco-Core-Property-Value-Converters is updated to latest

and also in umbracoSettings.config <EnablePropertyValueConverters>true</EnablePropertyValueConverters> is set to true

then access your media url via
archtypePanel.GetPropertyValue<IPublishedContent>("imageToDisplay").Url

Access using udi url

another way is by using the udi url you have got and get media like:

// Your string which is retrieved from Archetype.
var imageString = "umb://media/c33bfe07a82b4df18a79db154139cb91";

// Get the guid from this string.
var imageGuidUdi = GuidUdi.Parse(imageString);

// Get the ID of the node!
var imageNodeId = ApplicationContext.Current.Services.EntityService.GetIdForKey(guidUdi.Guid, (UmbracoObjectTypes)Enum.Parse(typeof(UmbracoObjectTypes), guidUdi.EntityType, true));

// Finally, get the node.
var imageNode = Umbraco.TypedMedia(imageNodeId.Result);

more info about this, checkout Umbaco Archtype rendering images (MediaPicker2)

2
  • Hi @Alan Tsai thanks for the reply, seems in my case the install of a new Umbraco was corrupt, I saved the project to TFS and then removed all Nuget packages and then re-installed them. I then compared config files and old version had lots of attributes and elements missing. It now works with: ImageId = archtypePanel.GetValue<IPublishedContent>("imageToDisplay").Url which did not work previously Jul 25, 2017 at 15:39
  • Umbraco.TypedMedia supports a Udi as a parameter, no need for that extra call to GetIdForKey. This is simpler and will accomplish the same thing: var imageNode = Umbraco.TypedMedia(imageGuidUdi); Feb 27, 2018 at 2:01
0

You Can Try this:

@using Umbraco.Web.Extensions
string HeaderImage = "umb://media/c740e797fc424980afb087769caf0917";
Udi udi;
if (Udi.TryParse(HeaderImage.ToString(), out udi))
{
  var mediaItem = udi.ToPublishedContent();
  string ImageURL = mediaItem.Url;
      <div class="col-sm-3">
           <img src="@ImageURL">
      </div>
}

(Source)

0

The below code worked for me:

var countryUrl = "umb://document/72e07554a09d4d8ab6af7ad926c9a3c4";                   
var countryGuidUdi = GuidUdi.Parse(countryUrl);  

var countryNodeId = ApplicationContext.Current.Services.EntityService.GetIdForKey(countryGuidUdi.Guid, (UmbracoObjectTypes)Enum.Parse(typeof(UmbracoObjectTypes), countryGuidUdi.EntityType, true));

var countryName = Umbraco.TypedContent(countryNodeId.Result).Name;

var countryCode = Umbraco.TypedContent(countryNodeId.Result).GetProperty("countryCode").Value;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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