Copied to clipboard

Flag this post as spam?

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


  • Nigel Wilson 945 posts 2077 karma points
    Jan 04, 2012 @ 04:20
    Nigel Wilson
    0

    Programmatically Add Media Item to MultiNode Tree Picker

    Hi there

    I have a scenario where I have the MultiNodeTreePicker setup as a property on a Document Type for associating related files.

    I am currently working on a user control that will

     

    • Enable the user to create new content nodes.
    • Enable the user to upload 'n' media items and have them automagically be associated with the node.

    So far I have the following working:

    • New content node being created
    • A listing of any existing media items - my code for this is shown below:
    • The ability to upload a new media item
    lstFiles.Items.Clear();
    string pickerValue = string.Empty;
    Property pickerProperty = new Node(Convert.ToInt32(ViewState["nodeID"].ToString())).GetProperty("documents");
    if (pickerProperty != null)
    {
    pickerValue = pickerProperty.Value;
    }
    if (string.IsNullOrEmpty(pickerValue))
    {
    return false;
    }
    using (XmlReader xmlReader = XmlReader.Create(new StringReader(pickerValue)))
    {
    xmlReader.Read();
    if (xmlReader.Name == "MultiNodePicker")
    {
    // Position on first <nodeId>
    xmlReader.ReadStartElement();
    while (!xmlReader.EOF)
    {
    if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "nodeId")
    {
    lstFiles.Items.Add(new ListItem(new Media(xmlReader.ReadElementContentAsInt()).Text));
    }
    else
    {
    // Step the reader on
    xmlReader.Read();
    }
    }
    }
    }
    lstFiles.Visible = true;

    The above code I found within Umbraco Forums - sorry I cannot find the original post at the moment.

    What I am struggling with is how do I then link the new media item to the content node within the MultiNode Tree Picker (data stored as XML).

    Can anyone enlighten please ?

    Thanks

    Nigel

  • Rich Green 2246 posts 4008 karma points
    Jan 04, 2012 @ 08:05
    Rich Green
    0

    Hi Nigel,

    You might find it easier to set the MultiNodeTreePicker to 'csv', then it's just a case of building up a string (getting a list of media ids, like 1293,3245,6354) and splitting them with a comma.

    Then you just need to set the Doc Type property to this string.

    Rich

  • Nigel Wilson 945 posts 2077 karma points
    Jan 04, 2012 @ 08:50
    Nigel Wilson
    0

    Hi Rich

    Thanks for that and yes the thought had crossed my mind.

    I was hoping to avoid changing it to CSV, as it means I would then have to recode some other parts of the site.

    So I will work out which is going to be the more painful tomorrow when I am back at work and go from there... 

    Cheers

    Nigel

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jan 04, 2012 @ 10:57
    Lee Kelleher
    0

    Hi Nigel,

    The code example you gave was only for reading the values from the MNTP, but to write data back to the property you'll need to go via the Document object API.

    Here is a quick example:

    var nodeId = 1234; // get the node id from wherever - ViewState, etc.
    var doc = new umbraco.cms.businesslogic.web.Document(nodeId);
    if (doc != null)
    {
        var property = doc.getProperty("documents");
        if (property != null)
        {
            var mediaId = 5678; // not sure where you get the media id from?
            var value = property.Value.ToString();
            property.Value = value.Replace("</MultiNodePicker>", string.Concat("<nodeId>", mediaId, "</nodeId></MultiNodePicker>"));
        }
    
        doc.Save();
        doc.Publish(umbraco.BusinessLogic.User.GetUser(0)); // or specify the user?
        umbraco.library.UpdateDocumentCache(doc.Id);
    }

    Obviously the values for both nodeId and mediaId aren't real, you'd need to plug those in yourself.  You might think that the string replace way is a little crude, but if you are just adding a single value to the MNTP, then it's the quickest way.  The alternative is to load/parse the value into an XmlDocument, append the <nodeId> element and flatten it back into a string ... an unfair amount of overhead especially when you are ultimately dealing with a string value. :-)

    Good luck!

    Cheers, Lee.

  • Nigel Wilson 945 posts 2077 karma points
    Jan 04, 2012 @ 19:41
    Nigel Wilson
    0

    Hi Lee

    Thanks for that...

    The user will be uploading a single file at a time and they are only likley to upload 2 or 3 at most.

    So I am thinking that I will do a bit of string manipulation as you suggest - this will avoid recoding other parts of the site.

    Thanks for the code snippet.

    Cheers

    Nigel

Please Sign in or register to post replies

Write your reply to:

Draft