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 214 posts 506 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 31 posts 165 karma points
    Dec 01, 2017 @ 18:31
    Johan Reitsma
    0

    you must use media.key to get a UDI

  • anthony hall 214 posts 506 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 31 posts 165 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 214 posts 506 karma points
    Dec 04, 2017 @ 11:14
    anthony hall
    2

    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 1457 posts 5606 karma points MVP 3x c-trib
    Dec 04, 2017 @ 13:29
    Dan Diplo
    5

    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.

  • Johan Reitsma 31 posts 165 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