Copied to clipboard

Flag this post as spam?

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


  • Thomas Egebrand Gram 63 posts 138 karma points
    May 16, 2017 @ 12:53
    Thomas Egebrand Gram
    1

    Setting the value of MultiNodeTreePicker2 programatically

    Hello there!

    Been searching and trying to implement this for some time now. I would like to programatically set the value of a Umbraco.MultiNodeTreePicker2 datatype.

    Here's the code I've tried:

    /// <summary>
    /// Create an Exercise
    /// </summary>
    /// <param name="parentId"></param>
    /// <param name="model"></param>
    private void CreateExercise(int parentId, Exercise model)
    {
        // UserId of the Umbraco admin
        int userId = 0;
        // Get the Umbraco contentService
        var cs = Services.ContentService;
    
        // Create the node
        var content = cs.CreateContent(model.Name, parentId, "Exercise", userId);
    
        // Set custom properties
        content.SetValue("content", model.Content);
        content.SetValue("imageUrl", model.ImageUrl);
    
        // Entering as a CSV
        var testArray = model.BodyParts.Select(b => b.Id).ToArray();
        content.SetValue("bodyParts", string.Join(",", testArray));
    
        // Entering as a IENumerable<IPublishedContent>
        //content.SetValue("bodyParts", model.BodyParts);
    
        // Save the node
        cs.SaveAndPublishWithStatus(content);
    }
    

    Unfortunately, neither CSV or IENumerable

    What sort of datatype should the data be when setting the value of the MultiNodeTreepicker?

    Thanks!

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 16, 2017 @ 17:08
    Alex Skrypnyk
    0

    Hi Thomas

    I think you need to set list of "Udi"s

    Udi is new format of ids, and MultiNodeTreePicker2 stores data like list if Udis.

    Thanks,

    Alex

  • Thomas Egebrand Gram 63 posts 138 karma points
    May 16, 2017 @ 18:01
    Thomas Egebrand Gram
    0

    Hey there Alex, thanks for your reply.

    That spurs another question; how does one get the UDI of an IPublishedContent? Documentation does't seem to define this anywhere yet.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 21, 2017 @ 20:51
    Alex Skrypnyk
    0

    Hi Thomas

    Did you solve this issue?

    Can you share with the community?

    Thanks,

    Alex

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 16, 2017 @ 22:21
    Alex Skrypnyk
    102

    Hi Thomas

    It should be something like:

    var node =
    ApplicationContext.Services.ContentService.GetById(3232);
    
    var locaUdi = Udi.Create(Constants.UdiEntityType.Document, node.Key);
    

    IPublishedContent returns empty Guid, but it works with an entity from the database.

    Thanks,

    Alex

  • Thomas Egebrand Gram 63 posts 138 karma points
    May 22, 2017 @ 11:56
    Thomas Egebrand Gram
    5

    Thank you Alex, solved the issue.

    This is how i ended up implementing it:

    // Get all the body parts
    IEnumerable<IContent> bodyParts = cs.GetDescendants(1334);
    
    // Construct the array of strings to search for in Umbraco, then create a list of the UDI's
    string[] bodyPartsSearch = new string[] { "Fødder", "Ben" };
    model.BodyParts = bodyParts.Where(b => bodyPartsSearch.Contains(b.Name)).Select(n => Udi.Create(Constants.UdiEntityType.Document, n.Key));
    

    Then, in the CreateExercise method (to save it), i did this:

    var bodyPartsArray = model.BodyParts.Select(b => b.ToString()).ToArray();
    var bodyParts = string.Join(",", bodyPartsArray);
    
    content.SetValue("bodyParts", bodyParts); // Should look like this: "umb://document/9ac2abf298964ec2b660adf6bd2e6877,umb://document/9f886d3aa0794990bcf795114b3203c4"
    

    Have an awesome day!

    // Thomas

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 22, 2017 @ 12:11
    Alex Skrypnyk
    0

    Hi Thomas

    Glad that you solved this issue!!! Have a great day too.

    Alex

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jun 05, 2017 @ 12:43
    Dan Diplo
    5

    Just encountered this. I would have expected some helper methods in the ContentService to deal with this, but in the meantime I wrote these two handy extension methods that convert a list of content to a CSV string suitable for using with content service:

    /// <summary>
    /// Converts a list of published content to a comma-separated string of UDI values suitable for using with the content service
    /// </summary>
    /// <param name="content">The published content</param>
    /// <returns>A CSV string of UID values eg. umb://document/56c0f0ef0ac74b58ae1cce16db1476af,umb://document/5cbac9249ffa4f5ab4f5e0db1599a75b</returns>
    public static string ToUdiCsv(this IEnumerable<IPublishedContent> content)
    {
        return String.Join(",", content.Select(c => Umbraco.Core.Udi.Create(Umbraco.Core.Constants.UdiEntityType.Document, c.GetKey())));
    }
    
    /// <summary>
    /// Converts a list of content to a comma-separated string of UDI values suitable for using with the content service
    /// </summary>
    /// <param name="content">The content</param>
    /// <returns>A CSV string of UID values eg. umb://document/56c0f0ef0ac74b58ae1cce16db1476af,umb://document/5cbac9249ffa4f5ab4f5e0db1599a75b</returns>
    public static string ToUdiCsv(this IEnumerable<IContent> content)
    {
        return String.Join(",", content.Select(c => Umbraco.Core.Udi.Create(Umbraco.Core.Constants.UdiEntityType.Document, c.Key)));
    }
    

    Usage is something like:

    var cs = Services.ContentService;
    
    var nodes = Umbraco.TypedContent(1234).Children(); // IEnumerable<IPublishedContent> 
    
    var content = cs.GetById(567);
    
    content.SetValue("content",  nodes.ToUdiCsv());
    
  • Tito 314 posts 623 karma points
    Jun 08, 2017 @ 15:21
    Tito
    0

    I think IPublishedContent GetKey() gets an empty GUID, i dont know why. It should work with IContent.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jun 08, 2017 @ 20:03
    Dan Diplo
    0

    Seems to work OK with IPublishedContent when I've tried it....

Please Sign in or register to post replies

Write your reply to:

Draft