Try our conversational search powered by Generative AI!

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:
 

This should do the trick for you:

https://world.episerver.com/Documentation/Items/Developers-Guide/EPiServer-CMS/7/Content/Pages-and-Blocks/How-To/Edit-hints-in-MVC/

#189866
Mar 26, 2018 21:29
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
Vote:
 

Does anyone found and answer for this? Looking at similar problem.

I've been able to tell that that after save and event completing, server returns field with updated value. So seems like some events on UI are not taking that information into account.

#228415
Edited, Sep 25, 2020 10:00
Vote:
 

I've recently come accross this issue was well, has anyone come up with a resolution?

#249263
Feb 25, 2021 23:20
Vote:
 

Here is a solution to this issue.

https://world.optimizely.com/blogs/bartosz-sekula/dates/2021/7/refresh-current-editing-context-on-property-value-change/

Please note, this is supported in CMS UI 11.36.0  and greater as mentioned.

#283218
Jul 06, 2022 6:08
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.