Copied to clipboard

Flag this post as spam?

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


  • Lewis Smith 211 posts 620 karma points c-trib
    Apr 20, 2018 @ 11:18
    Lewis Smith
    0

    System.FormatException: String "3553" is not a valid udi.

    Hi,

    I'm programmatically adding pages. These are being added successfully, but once trying to load the page on the website I get the following error:

    System.FormatException: String "3553" is not a valid udi.
    

    This error is on the first image that the page loads. The error in this case is on:

    IPublishedContent image = item.GetPropertyValue<IPublishedContent>("mainImage");
    

    The solution to this is to manually go into the CMS and click save and publish on the item which then loads the page without any errors and with all the images as expected...

    Where am I going wrong?

    Thanks, Lewis

  • Lewis Smith 211 posts 620 karma points c-trib
    Apr 23, 2018 @ 07:48
    Lewis Smith
    0

    anyone?

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 23, 2018 @ 08:10
    Michaël Vanbrabandt
    0

    Hi Lewis,

    umbraco introces the UDI identifier format from version 7.6.0+. You can find more information about the format in the docs:

    https://our.umbraco.org/documentation/reference/querying/Udi

    Can you show use the code where you add a content page programmatically?

    Normally you should use something like:

    var pageUdi = page.GetUdi();
    

    to get the Udi identifier of a published content object. Then to set this onto another page:

    otherPage.SetValue("pageAlias", pageUdi.ToString());
    

    Hope this helps!

    /Michaël

  • Lewis Smith 211 posts 620 karma points c-trib
    Apr 23, 2018 @ 08:45
    Lewis Smith
    0

    Hi Michael,

    There is a fair amount of code but here is the main part of it that i think you will be interested in...

    IContent Page = null;
    Page = this.Services.ContentService.CreateContent(node.NodeTitle, this.Services.ContentService.GetById(node.ParentId), node.Alias);
    
    
    //Upload images for product
    var FolderId = GetFolderIdFromCreation(node.Reference);
    var MainImageId = UploadImage(FolderId, node.MainImageUrl, "MainImage-" + node.Reference);
    if (MainImageId != 0)
    {
        var MainImageString = MainImageId.ToString();
        var FormattedPrice = Int32.Parse(node.PriceGBP.Replace(".00", "").Replace(",", "").Replace(".", ""));
        var OtherImage1 = UploadImage(FolderId, node.OtherImage1Url, "OtherImage1-" + node.Reference);
        var OtherImage2 = UploadImage(FolderId, node.OtherImage2Url, "OtherImage2-" + node.Reference);
        var OtherImage3 = UploadImage(FolderId, node.OtherImage3Url, "OtherImage3-" + node.Reference);
    
        //Required
        Page.SetValue("mainImage", MainImageString);
        Page.SetValue("gender", 123);
    
        //Optional         
        Page.SetValue("secondImage", OtherImage1.ToString());
        Page.SetValue("thirdImage", OtherImage2.ToString());
        Page.SetValue("fourthImage", OtherImage2.ToString());                                    
    
        try
        {                                       
            Services.ContentService.SaveAndPublishWithStatus(Page);
            ProductService.Instance.SetStock(1, Page.Id.ToString(), 1);
            SuccessList.Add(node);
        }
        catch (Exception)
        {
             ErrorList.Add(node);
             continue;
        }
    }
    else
    {
         ErrorList.Add(node);
    }
    

    This code is in a foreach loop hence the continue.

    The values that are set for the images are (int) ids of the images that are uploaded.

    The alias is set at the top (node.Alias) which is just a string of "watchProduct"

    Thanks, Lewis

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 23, 2018 @ 08:53
    Michaël Vanbrabandt
    0

    Hi Lewis,

    so you are using MediaPicker for these images correct?

    In your code you have:

    var MainImageId = UploadImage(FolderId, node.MainImageUrl, "MainImage-" + node.Reference);
    

    Which contains the id of the main image, using this Id get the typed media object using:

    var mainMediaImg = Services.MediaService.GetById(MainImageId);
    

    Then to get the Udi identifier use:

    var mainMediaUdi = mainMediaImg.GetUdi();
    

    Then set this value:

    Page.SetValue("mainImage", mainMediaUdi.ToString());
    

    Hope this helps!

    PS: code not tested, manually written here!

    /Michaël

  • Lewis Smith 211 posts 620 karma points c-trib
    Apr 23, 2018 @ 09:01
    Lewis Smith
    0

    Hi Michael,

    So i have the following:

    var MainImageId = 123; //normally the id of the image that has just been uploaded.
    var MainMediaUdi = Umbraco.TypedMedia(MainImageId.ToString());
    Page.SetValue("mainImage", MainMediaUdi.GetUdi());
    

    This issue is im getting is 'IPublishedContent' does not contain a definition for 'GetUdi' and no extension method 'GetUdi' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)

    So it seems i getting IPublishedContent, is this a TypedMediaObject?

    Thanks, Lewis

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 23, 2018 @ 09:02
    Michaël Vanbrabandt
    0

    Lewis,

    yes just changed my answer, was typing to fast. You need the MediaService to get the media file.

    Hope this helps!

    /Michaël

  • Lewis Smith 211 posts 620 karma points c-trib
    Apr 23, 2018 @ 09:43
    Lewis Smith
    0

    Hi Michael,

    So when the code hits the part when it runs the GetUdi() it throws an error of "The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments"

    The image is being added as i expect it to be, its also returning the Media MediaService item as expected...

    thanks, Lewis

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 23, 2018 @ 09:46
    Michaël Vanbrabandt
    0

    Hi Lewis,

    you forgot to cast it to ToString() see my post above.

    Hope this helps!

    /Michaël

  • Lewis Smith 211 posts 620 karma points c-trib
    Apr 23, 2018 @ 09:49
    Lewis Smith
    1

    -_- Work a treat! I will buy you a drink at codegarden this year!

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 23, 2018 @ 09:50
    Michaël Vanbrabandt
    1

    Hi Lewis,

    no problem!

    I won't make it this year, but you can save the drink for another time when we meet!

    Have a nice day

    /Michaël

Please Sign in or register to post replies

Write your reply to:

Draft