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?
// 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"
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());
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:
Unfortunately, neither CSV or IENumerable
What sort of datatype should the data be when setting the value of the MultiNodeTreepicker?
Thanks!
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
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.
Hi Thomas
Did you solve this issue?
Can you share with the community?
Thanks,
Alex
Hi Thomas
It should be something like:
IPublishedContent returns empty Guid, but it works with an entity from the database.
Thanks,
Alex
Thank you Alex, solved the issue.
This is how i ended up implementing it:
Then, in the CreateExercise method (to save it), i did this:
Have an awesome day!
// Thomas
Hi Thomas
Glad that you solved this issue!!! Have a great day too.
Alex
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:Usage is something like:
I think IPublishedContent GetKey() gets an empty GUID, i dont know why. It should work with IContent.
Seems to work OK with IPublishedContent when I've tried it....
is working on a reply...