Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • anthony hall 222 posts 536 karma points
    Dec 01, 2017 @ 17:37
    anthony hall
    1

    is not a valid udi - when saving media using content service

    The following code successfully creates a node that references a media item. Everything looks as in intended in the back-office. However, in my view I get the following exception.

    String "5317" is not a valid udi.

    if I save and published each node via the back-office, then the view renders as expected. I recall that I need to flush the cached. What am i doing wrong here?

            private void Save()
        {
            var contentService = ApplicationContext.Current.Services.ContentService;
            var content = contentService.CreateContent("testnode", 1234, "docType");
    
    
            content.SetValue("summary", "hello world");
    
            if (!string.IsNullOrEmpty(mediaUrl))
            {
                var media = CreateMedia(tweet, _twitterMedia.Id, mediaUrl);
                if (media != null)
                {
                    content.SetValue("image", media.Id);
                }
            }
    
            contentService.SaveAndPublishWithStatus(content, 0, false);
        }
    
        private Umbraco.Core.Models.IMedia CreateMedia(int mediaId, string mediaUrl)
        {
            var mediaService = ApplicationContext.Current.Services.MediaService;
            var request = WebRequest.Create(mediaUrl);
            var webResponse = request.GetResponse();
            var responseStream = webResponse.GetResponseStream();
            Umbraco.Core.Models.IMedia media = null;
            if (responseStream != null)
            {
                var originalImage = new Bitmap(responseStream);
                var path = HttpContext.Current.Server.MapPath("~/App_Data/TempImage/test.jpg");
                originalImage.Save(path, ImageFormat.Jpeg);
                using (FileStream fileStream = new FileStream(path, FileMode.Open))
                {
                    var fileName = fileStream.Name;
                    var mediaImage = mediaService.CreateMedia(tweet.Id.ToString(), mediaId, "image");
                    mediaImage.SetValue("umbracoFile", fileName, fileStream);
                    mediaService.Save(mediaImage);                    
                    responseStream.Dispose();
                    webResponse.Dispose();
                    originalImage.Dispose();
                    media = mediaService.GetById(mediaImage.Id);
                    mediaService.RebuildXmlStructures(mediaImage.Id);
                }
    
            }
            return media;
        }
    
  • Johan Reitsma 67 posts 233 karma points
    Dec 01, 2017 @ 18:31
    Johan Reitsma
    0

    you must use media.key to get a UDI

  • anthony hall 222 posts 536 karma points
    Dec 04, 2017 @ 10:03
    anthony hall
    0

    I think the error about the UDI is a red herring. As the item is saved to the media library. I've tried using media.Key but this throws an exception. I feel the answer lies in rebuilding the cache or republishing via the content service.

  • Johan Reitsma 67 posts 233 karma points
    Dec 04, 2017 @ 10:23
    Johan Reitsma
    101

    I'm sorry, the code i use is var media = ms.GetById(id); if (media != null) { resave = true; //(" --> umb://media/" + media .Key) return "umb://media/" + media .Key.ToString().Replace("-", ""); }

    We use this in our upgrade script from old obsolete pickers to the new version.

  • anthony hall 222 posts 536 karma points
    Dec 04, 2017 @ 11:14
    anthony hall
    3

    Thanks Johan. That's it!

    // media from ContentService

     if (media != null)
                        {
                            var mediaKey = "umb://media/" + media.Key.ToString().Replace("-", "");
                            content.SetValue("image", mediaKey);
                        }
    
  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Dec 04, 2017 @ 13:29
    Dan Diplo
    6

    The "proper" way to do this (without concatenating and escaping strings would be):

    var udi = Udi.Create(Constants.UdiEntityType.Media, media.Key);
    
    content.SetValue("image", udi.ToString());
    

    (The Constants class is in Umbraco.Core namespace).

  • Chris 12 posts 84 karma points
    Oct 19, 2018 @ 16:18
    Chris
    0

    Thank you, this fixed the issue where the image would not show up on the media picker in the back end.

  • Dmitriy 168 posts 588 karma points
    Feb 19, 2020 @ 15:21
    Dmitriy
    0

    I think you answer must be set as correct!

    Thanks, Dan

    Oh, by the way for others, there is an other way to get an UDI:

    // IMedia extension method
    media.GetUdi()
    
  • Johan Reitsma 67 posts 233 karma points
    Dec 04, 2017 @ 14:25
    Johan Reitsma
    0

    Learned something new. Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft