Copied to clipboard

Flag this post as spam?

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


  • Remi Martel 32 posts 104 karma points
    Apr 23, 2018 @ 14:04
    Remi Martel
    0

    Set multinode treepicker value programmatically in Umbraco 7.10

    How do I set the value for a multinode treepicker in c#. I found the post from 2015: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/71722-setvalue-in-multi-node-tree-picker but it doesn't seem to work. Here is my code:

                    var doc = contentServices.GetById(item.Id);
                    doc.SetValue("schedule", page.Id+",7190");
                    contentServices.SaveAndPublishWithStatus(doc);
                    umbraco.library.RefreshContent();
                    var asd = doc.GetValue("schedule");
    

    I get 'doc' properly and when I check the value of 'asd' with a breakpoint, it is as I set it with 'SetValue', but when i finish the process and look at my backoffice, multinode treepicker is still empty.

  • Sérgio 30 posts 171 karma points
    Aug 06, 2018 @ 12:30
    Sérgio
    1

    I'm building it based on its GUID:

    List<string> otherCategoriesList = new List<string>();
    foreach (var otherCategory in post.OtherCategories) {
    
        IPublishedContent catNode = rootNode.Descendants().Where(x => x.Name == otherCategory).FirstOrDefault();
        var guid = contentService.GetById(catNode.Id).GetUdi().Guid;
        otherCategoriesList.Add(string.Concat("umb://document/", guid.ToString().Replace("-", "")));
    }
    
    postContent.SetValue("otherCategories", string.Join(",", otherCategoriesList));
    
  • Puck Holshuijsen 188 posts 751 karma points
    Aug 08, 2019 @ 10:02
    Puck Holshuijsen
    2

    Thanks for this answer, saved me some time ;)

    Btw, you can make your code a little bit faster and safer like this:

     List<string> otherCategoriesList = new List<string>();
                    IEnumerable<IPublishedContent> descendants = rootNode.Descendants();
                    foreach (var otherCategory in post.OtherCategories) {
    
                        IPublishedContent catNode = descendants.FirstOrDefault(x => x.Name == otherCategory);
                        if (catNode != null) {
                            otherCategoriesList.Add(contentService.GetById(catNode.Id).GetUdi().ToString());
                        }
                    }
    
                    postContent.SetValue("otherCategories", string.Join(",", otherCategoriesList));
    
  • Nick Hoang 52 posts 182 karma points c-trib
    Sep 27, 2019 @ 08:08
    Nick Hoang
    1

    A note that the guid key shouldn't include - chars

    For example when you get some records by contentService:

    var contents = Services.ContentService.GetByIds([Ids]);
    

    Then you want to get the value to set to multinode treepicker property, you will do this:

    var valueToSet = string.Join(",", contents.Select(e => "umb://document/" + e.Key.ToString().Replace("-", ""))); 
    

    Hope it help.

    Cheers.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies