SavingContent event - update block content - values not updated

Vote:
 

Hi!

I am updating content on a block in the SavingContent event.

    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class BlockEventsInitialization : IInitializableModule
    {
        private IContentEvents _events;

        public void Initialize(InitializationEngine context)
        {
            _events = ServiceLocator.Current.GetInstance();

            _events.SavingContent += SavingContent;
        }

        private void SavingContent(object sender, ContentEventArgs e)
        {
            var block = e.Content as MyTestBlock;

            if (block != null)
            {
                block.Text = "New text";

                e.Content = block as IContent;
            }
        }

        public void Uninitialize(InitializationEngine context)
        {
            _events.SavingContent -= SavingContent;
        }
    }

In my case I am replacing blocks in a ContentArea, this is just a small example with the same result.

The Text property does not say "New text" until I reload the page, hitting "Ready to Publish", actually publish the block or go to another block/page and then go back the block I am updating.

I do not want to publish my changes, just save.

Anyone have an idea how to update values without reloading or publish the block?

/Magnus

#189844
Edited, Mar 26, 2018 12:57
Vote:
 

Hi Magnus

You need to call CreateWritableClone and update the returned instance. By default all content is readonly for performance. I'm actually a little surprised an exception wasn't thrown. Something like this should do the trick:

var updateBlock = block.CreateWritableClone();
updateBlock.Text = "New Text";
e.Content = updateBlock as IContent;

David

#189867
Mar 26, 2018 22:05
Vote:
 

Thanks for the response Aniket and David!

Setting e.Content with a CreateWritableClone() gives the same result, David.

Simplest possible scenario gives the same result:

        private void SavingContent(object sender, ContentEventArgs e)
        {
            e.Content.Name = "New name";
        }

The new value is not presented in the "All Properties" view until I do one of these:

  • A page refresh
  • Ready to Publish
  • Publish Changes
  • Navigate to another page/block and back

PS. Just to be clear; I am only referring to the "All Properties"-view and not "On-Page Editing"-view.

Best regards
Magnus

#189868
Edited, Mar 26, 2018 22:57
Vote:
 

Hey Magnus

Sorry I mis-read your original post. Will have a think about your issue and get back to you. 

David

#189869
Mar 26, 2018 23:21
Vote:
 

might be related to the case when value set on the client side is not the same that was mutated on the server. resulting in some weird dojo-mojo caching thingies?!

#189939
Mar 27, 2018 16:55
Vote:
 

Did anyone find a solution to this? I'm attempting similar property changes on SavingConent and not having luck with the All Properties view refreshing afterwards

#204916
Jun 20, 2019 18:07
Vote:
 

Valdis, I think weird dojo-mojo caching thingies is going to be the name of my next band. Lol!

#204940
Jun 21, 2019 15:56