Copied to clipboard

Flag this post as spam?

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


  • Daan Stolp 6 posts 85 karma points
    3 days ago
    Daan Stolp
    0

    How to programmatically add content to a Block List?

    I am building an Umbraco 15 application.

    • The application contains a page of type Countries.
    • The page has a property called countryList, which is a Block List Editor.
    • It contains blocks with a DocumentType of 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.

    CountryPage page = _umbracoHelper.Content(CountryPageId()) as CountryPage;
    BlockListModel blockList = page.CountryList;
    

    Can anyone point me in the right direction?

  • Afreed 64 posts 277 karma points
    2 days ago
    Afreed
    2

    Hi Daan,

    This is how we did in v13.

      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");
    
  • Daan Stolp 6 posts 85 karma points
    4 hours ago
    Daan Stolp
    0

    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!

Please Sign in or register to post replies

Write your reply to:

Draft