public string GetBlockListJsonFor<T>(IEnumerable<T> items, Guid contentTypeKey, List<Dictionary<string, string>> settingsData = null) where T : class
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
Blocklist blocklistNew = new Blocklist();
var contentList = new List<Dictionary<string, string>>();
var dictionaryUdi = new List<Dictionary<string, string>>();
foreach (var item in items)
{
var itemList = item as Location;
var udi = new GuidUdi("element", Guid.NewGuid()).ToString();
PropertyInfo[] props = item.GetType().GetProperties();
var propertyValues = new Dictionary<string, string>()
{
{ "contentTypeKey", contentTypeKey.ToString() },
{ "udi", udi.ToString() },
{"city",itemList.City},
{"street",itemList.Street },
{"postCode",itemList.PostCode },
{"LocationId",itemList.Id.ToString() },
{ "coordinatesX", itemList.Coordinates?.Count > 0 ? itemList.Coordinates[0].ToString() : string.Empty },
{ "coordinatesY", itemList.Coordinates?.Count > 1 ? itemList.Coordinates[1].ToString() : string.Empty },
};
foreach (PropertyInfo prop in props)
{
object[] attrs = prop.GetCustomAttributes(true);
foreach (object attr in attrs)
{
if (attr is JsonPropertyAttribute attribute)
{
propertyValues.Add(attribute.PropertyName, prop.GetValue(item)?.ToString() ?? string.Empty);
}
}
}
contentList.Add(propertyValues);
dictionaryUdi.Add(new Dictionary<string, string> { { "contentUdi", udi } });
}
blocklistNew.Layout = new BlockListUdi(dictionaryUdi);
blocklistNew.ContentData = contentList; //contentData is a list of our object
blocklistNew.SettingsData = settingsData ?? new List<Dictionary<string, string>>();
return JsonConvert.SerializeObject(blocklistNew);
}
};
Make sure you are using your blocklist contentType Guid
var json = GetBlockListJsonFor(therapist.Locations, new Guid("faeccfe7-ebea-4461-9caa-cc9e3541c969"));
therapistNode.SetValue("locations", json, culture: "en-US");
Thanks Afreed, but it seems that in v15, all the type definitions for the Block List model have changed. None of the types that you rely on (Blocklist, BlockListUdi,etc.) exist anymore, and a bunch of new ones have been introduced.
There's about 100 relatively small content items that I need to import (once). At this point, I'm seriously considering doing that manually through the backoffice, as that will probably be faster than figuring this out.
I really like Umbraco, but this is one area where they can seriously improve the developer experience (and/or the docs)...
How to programmatically add content to a Block List?
I am building an Umbraco 15 application.
Countries
.countryList
, which is a Block List Editor.Country
.I want to add new 'countries' to this Block List programatically (i.e. add new content to the Block List). How do I achieve this?
I currently retrieve a reference to the page and the property using the following code, but then I'm at a loss how to add content to the list.
Can anyone point me in the right direction?
Hi Daan,
This is how we did in v13.
Make sure you are using your blocklist contentType Guid
Thanks Afreed, but it seems that in v15, all the type definitions for the Block List model have changed. None of the types that you rely on (
Blocklist
,BlockListUdi
,etc.) exist anymore, and a bunch of new ones have been introduced.There's about 100 relatively small content items that I need to import (once). At this point, I'm seriously considering doing that manually through the backoffice, as that will probably be faster than figuring this out.
I really like Umbraco, but this is one area where they can seriously improve the developer experience (and/or the docs)...
Anyway, thanks again for your input!
is working on a reply...