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.
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. :-)
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
So far I have the following working:
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
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
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
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:
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.
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
is working on a reply...