Copied to clipboard

Flag this post as spam?

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


  • Sibren 39 posts 211 karma points c-trib
    Jun 22, 2021 @ 19:27
    Sibren
    0

    ModelsBuilder + Block List not regenerating models without restart

    Hi,

    This is the setup:

    • I have some compositioned pages with a Block List
    • Adding a new block list item to the Block List Datatype
    • Adding the new block item to a content page, save and publish

    Then I go into the backend, debug the code:

    public Dictionary<string, object> GetPageContent(IPublishedContent publishedContent)
        {
            var dictionary = GetSettings(publishedContent.Id, publishedContent.Url(), publishedContent.Properties, publishedContent.ContentType.Alias);
    
            return dictionary;
        }
    
        public Dictionary<string, object> GetSettings(int id, string url, IEnumerable<IPublishedProperty> publishedProperties, string docType = "")
        {
            var dictionary = new Dictionary<string, object>();
            if (publishedProperties == null || !publishedProperties.Any()) return dictionary;
    
            foreach (var publishedProperty in publishedProperties)
            {
                var propertyType = publishedProperty.PropertyType.EditorAlias;
                if (propertyType == "Umbraco.MediaPicker3")
                {
                    if (publishedProperty.GetValue().GetType() == typeof(List<MediaWithCrops>))
                    {
                        var objectValues = (List<MediaWithCrops>)publishedProperty.GetValue();
                        var myData = new List<PublishedContentItem>();
                        foreach (var item in objectValues)
                        {
                            myData.Add(new PublishedContentItem(item));
                        }
                        dictionary.Add(publishedProperty.Alias, myData);
                    }
                    else if (publishedProperty.GetValue().GetType() == typeof(MediaWithCrops))
                    {
                        dictionary.Add(publishedProperty.Alias, new PublishedContentItem((MediaWithCrops)publishedProperty.GetValue()));
                    }
                }
                else if (propertyType == "Umbraco.ContentPicker")
                {
                    dictionary.Add(publishedProperty.Alias, new PublishedContentItem((IPublishedContent)publishedProperty.GetValue()));
                }
                else if (propertyType == "Umbraco.MultiNodeTreePicker")
                {
                    var objectValues = (IEnumerable<IPublishedContent>)publishedProperty.GetValue();
                    var myData = new List<PublishedContentItem>();
                    foreach (var item in objectValues)
                    {
                        myData.Add(new PublishedContentItem(item));
                    }
                    dictionary.Add(publishedProperty.Alias, myData);
                }
                else if (propertyType == "Umbraco.MultiUrlPicker")
                {
                    if (publishedProperty.GetValue().GetType() == typeof(IEnumerable<Link>))
                    {
                        var objectValues = (IEnumerable<Link>)publishedProperty.GetValue();
                        var myData = new List<MultiUrlPickerModel>();
                        foreach (var item in objectValues)
                        {
                            myData.Add(new MultiUrlPickerModel(item));
                        }
                        dictionary.Add(publishedProperty.Alias, myData);
                    }
                    else if (publishedProperty.GetValue().GetType() == typeof(Link))
                    {
                        dictionary.Add(publishedProperty.Alias, new MultiUrlPickerModel((Link)publishedProperty.GetValue()));
                    }
                }
                else if (propertyType == "Umbraco.TinyMCE")
                {
                    var value = publishedProperty.GetValue();
                    dictionary.Add(publishedProperty.Alias, value.ToString());
                }
                else if (propertyType == "Umbraco.Label")
                {
                    // do nothing with a label
                }
                else if (propertyType == "Umbraco.BlockList")
                {
                    var originalItems = (IEnumerable<BlockListItem>)publishedProperty.GetValue();
                    var customDictionary = new Dictionary<string, object>();
                    foreach (var blockListItem in originalItems)
                    {
                        var blockListItemDictionary = new Dictionary<string, object>();
                        blockListItemDictionary.Add("docType", blockListItem.Content.ContentType.Alias);
                        blockListItemDictionary.Add("content", GetSettings(0, "", blockListItem.Content?.Properties));
                        blockListItemDictionary.Add("settings", GetSettings(0, "", blockListItem.Settings?.Properties));
                        customDictionary.Add(blockListItem.Content.ContentType.Alias, blockListItemDictionary);
                    }
    
                    dictionary.Add(publishedProperty.Alias, customDictionary);
                }
                else
                {
                    dictionary.Add(publishedProperty.Alias, publishedProperty.GetValue());
                }
            }
    
            if (!string.IsNullOrEmpty(docType) && !dictionary.ContainsKey("docType"))
            {
                dictionary.Add("docType", docType);
            }
    
            if (id > 0)
            {
                dictionary.Add("id", id);
            }
    
            if (!string.IsNullOrEmpty(url))
            {
                dictionary.Add("url", url);
            }
    
            return dictionary;
        }
    }
    

    But it doesn't recognize my newly added block.

    What I've tried:

    • Adding a new property to an already existing doctype: works
    • Restarting the app, checking the new doctype is there: works
    • Changing the lifetime of the service I'm using to get all the content: doesn't work
    • Changing between Dll, LiveAppData, etc: doesn't work
    • Adding a new property to the new doctype (with ModelsBuilder as DLL and save and generate models): now it recognizes the item
    • Saving the new doctype. Clicking Save and Regenerate, adding it to the BlockList: not working.
    • Updating the new doctype by saving and regenerating (after the above): works

    My question is: how can I get this to happen automatically? I've got an intermediate user, who I trust to create new doctypes/compositions.

    Full source code: https://github.com/Sibren/Sibbo-Headless-U8

Please Sign in or register to post replies

Write your reply to:

Draft