#Save new variant of page when the page is being saved

1 messages · Page 1 of 1 (latest)

golden oak
#

I'm trying to implement a functionality in which I automatically set some fields for a different language based on what is entered for the published language.
When I'm saving a page I use a NotificationHandler and set the fields in a new culture based on the input.
However, whenever I save the page I get database write lock errors, even though I'm using a IScopedProvider.

Is anyone familiair with this or has someone perhaps done something similar before? I can't seem to get rid of the write locks

golden oak
#

Save new variant of page when the page is being saved

river yew
#

Will be easier to help if you include the code you wrote 🙂

golden oak
#

Thanks for your reply!
This is the code, there is a lot of logic going on here so i've abbreviated it a bit.

To clarify: this will actually work (i.e. all code is run), however, the regular publish call in Umbraco will throw an error because of a database lock so the new version still won't exist.

  {
        foreach (var content in notification.PublishedEntities)
        {
            if (content.ContentType.VariesByCulture())
            {
                var allLanguages = _localizationService.GetAllLanguages();
                var allCultures = allLanguages.Select(l => l.IsoCode).ToList();
                var currentCulture = content.CultureInfos?.Values.FirstOrDefault()?.Culture;

                foreach (var culture in allCultures)
                {
                    // do logic for each

                    CreateNewVersion()
                }
            }
        }
  }
#

And the function CreateNewVersion:

private void CreateNewCultureVersion(IContent content, string culture, Dictionary<string, string> newValues)
    {
        using (var scope = _scopeProvider.CreateScope())
        {

            try
            {

                // Retrieve existing content in the target culture, if it exists
                var existingContent = _contentService.GetById(content.Id);
                if (existingContent == null)
                {
                    // Handle case where content does not exist
                    return;
                }

                // Update properties with altered versions
                foreach (var property in existingContent.Properties)
                {  
                  existingContent.SetValue(property.Alias, newValues[property.Alias], culture);   
                }

                // Save the updated content for the target culture
                _contentService.Save(existingContent);
                scope.Complete();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Could not create new page variant");
                throw;
            }
        }
    }
river yew
#

I often get the database scope issues when mixing async methods and the Umbraco services - I know you said you slimmed it down, but are you sure your HandleAsync method needs to be async?

scenic monolith
#

another thing that could potentially help, is right now you go through each culture, and change/save the node for each culture. However on an IContent you can potentially just make the changes for all languages, and do 1 save operation instead of however many cultures you have. It would speed up the handler, and potentially you would have less points of failure for DB locks as well.

golden oak
golden oak
river yew