Prevent user from overriding changes made by content service

I have built something that adds an element to a blocklist through the content service.
When someone is editing this page while this is done, they will overwrite the added block by saving.
How do I prevent this person from saving when an element has been added through code and they have not yet refreshed?
We do want the user to be able to delete this element afterwards if wanted.

You could check for the presence of the custom added element with ContentService Notifications. See an example here ContentService Notifications Example | Umbraco CMS . If the element has been added from elsewhere, cancel the page save.

But if the user decides to delete the custom element, it would also be canceled. This is where I am kind of stuck.

Try adding a hidden property to the page’s document type as a flag. And set there if it’s allowed to save content or not by backoffice user. Set the flag to not allowed when you added the element from somewhere else from code. Then set it to allowed when needed.
Or compare version of the page using ContentService and content history. The version that is being saved by the user and the version that was modded by the code. And then decide if saving is allowed.
I’m not sure how your custom code works.

@warren created a package for this, but it’s only for Umbraco 15. But maybe he can give you some pointers on how to go about this. I’m not sure if it’s open source:

Edit: it is open source