Copied to clipboard

Flag this post as spam?

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


  • Mark Bellew 2 posts 82 karma points
    Feb 04, 2021 @ 13:09
    Mark Bellew
    0

    Updating Media Picker in Nested Content via JSON Serialization

    Hello everyone,

    I'm a Umbraco noob and this is my first post so forgive me if my terminology isn't accurate. I will get better with experience and I look forward to then being able to contribute to the forum.

    I'm trying to update a Nested Content element and I have it updating a dropdown, textbox, datetime and toggle (bool) data types, they are all working fine. However, I also need to update a media picker data type and I can't work out how to do this.

    In fact, I only really want to update one of the toggle properties ideally, but when I tried updating without the other property's data sent as part of the json string, it overwrote all the other property's data. So, first question, is there a way to update a nested content property and 'ignore' the other properties of the NC so that they do not get updated?

    Assuming all properties and their data are required to be sent in the json string, how can I update the media picker property (even though it will be updated with the same file data)?

    Below is the code that is working, for the above mentioned property data types. For clarity I removed the lines that were spitting out the data values to the screen so I know the below will retrieve the media picker id, url and name, I just need to know how to send those in the json string correctly, such that it will retain the correct media file (or know how to set it to ignore updating this property!!).

    PS: I hope the below is also useful to others because there's a lot of research and trial and error gone into getting this working thus far.

                    IContentService contentService = Services.ContentService;
                IMemberService memberService = Services.MemberService;
                var nodeID = Model.Id;
                var nodeKey = Model.Key;
                var memberKey = currMem.Key;
                var content = contentService.GetById(nodeKey);
                var member = memberService.GetById(currMemId);
                IEnumerable<IPublishedElement> publicationItems = currMem.Value<IEnumerable<IPublishedElement>>("publications");
                var ncItems = new List<Dictionary<string, string>>();
                int downloaded = 0;
                int subscribed = 0;
    
                if (publicationItems != null && publicationItems.Any()){
                    var pubSeries = "";
                    var pubKey = "";
                    foreach (var pubItem in publicationItems){
    
                        var alreadyDownloaded = pubItem.Value<bool>("alreadyDownloaded");
                        if (!alreadyDownloaded){
                            downloaded = 1;
                            <p>downloaded:: @downloaded</p>
                        } else {
                            <p>not downloaded:: @downloaded</p>
                        }
                        if(pubItem.Value<bool>("subscribedToPublication")){
                            subscribed = 1;
                        }
    
                        pubSeries = "[\""+pubItem.Value<string>("publicationSeries")+"\"]";
                        var dt = pubItem.Value<DateTime>("publicationUploadDate");
    
                        if (pubItem.HasProperty("publicationDocument") && pubItem.HasValue("publicationDocument")){
                            var mediaItem1 = pubItem.Value<IPublishedContent>("publicationDocument");
                            if (mediaItem1 != null)
                            {
                                var fileId1 = mediaItem1.Id;
                                var fileUrl1 = mediaItem1.Url;
                                var fileName1 = mediaItem1.Name;
                            }
                        }else{
                            <p>No file found</p>
                        }
    
                        ncItems.Add(new Dictionary<string, string>() {
                            {"key",$"{Guid.NewGuid()}"},
                            {"ncContentTypeAlias",pubItem.ContentType.Alias},
                            {"publicationSeries",@pubSeries},
                            {"publicationBespokeTitle",pubItem.Value<string>("publicationBespokeTitle")},
                            {"publicationMonthYear",pubItem.Value<string>("publicationMonthYear")},
                            {"subscribedToPublication",@subscribed.ToString()},
                            {"alreadyDownloaded",@downloaded.ToString()},
                            @* need to add media picker property here 
                                {"publicationDocument",WHAT GOES HERE?},
                            *@
                            {"publicationUploadDate",@dt.ToString("yyyy-MM-dd HH:mm:ss")}                           
                        });
                    }
                    var ncItemString = Newtonsoft.Json.JsonConvert.SerializeObject(ncItems);
                    member.SetValue("publications", ncItemString);
                    memberService.Save(member);                 
                }
    

    Example of this implementation of the nested content element

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Feb 04, 2021 @ 14:04
    Dan Diplo
    100

    Media items are stored in the following format as something called a UDI (Umbraco Document):

    umb://media/3b69765dfc3941b99ed81aba0cd1a251

    Where the last part is the "Key" which is the media's GUID.

    You can programatically create a UDI object like this:

    var udi = Udi.Create(Constants.UdiEntityType.Media, mediaItem1.Key)

    You'll need a reference to the Umbraco.Core namespace.

    Hope that is of some help?

  • Mark Bellew 2 posts 82 karma points
    Feb 04, 2021 @ 15:02
    Mark Bellew
    0

    Thanks Dan,

    That works a treat :-)

    Regards, Mark

Please Sign in or register to post replies

Write your reply to:

Draft