Updating a content item via code...

Topics: Core, Customizing Orchard, Writing modules
Oct 15, 2013 at 3:13 AM
I have a module with a custom content type and part. My part handler has some logic that needs to be executed in the OnUpdated() handler.

When I update a content item of this type via the UI, the OnUpdated handler is called correctly (as a result of the call to UpdateEditor() down in the bowels).

However I also have some application logic that updates my custom part that is associated with the content item. In this case, I would like the OnUpdated handler to fire, and I'm wondering what is the recommended approach here?

The other catch is that my part record is a ContentPartVersionRecord so ideally I want to maintain version history of these changes. I suspect that at present, when I simply fetch the part and change its properties via code, I'm not getting a new version?
Nov 3, 2013 at 4:24 PM
I faced the same problem when importing content items (https://orchard.codeplex.com/discussions/465227).

I modified the import to call OnUpdated - but I'm wondering if calling OnUpdated in ContentHandlers should be handled by core.
Feb 28, 2014 at 8:50 AM
I'm stuck with a similar issue. I would like to trigger the OnIndexing method.
Could you tell me how you called OnUpdated in your import code?
Feb 28, 2014 at 2:34 PM
Edited Feb 28, 2014 at 2:39 PM
We added some code to our fork of DefaultContentManager to trigger the OnUpdated event in import: (strange formatting auto applied here, but look at the •// --adenin: added to see the 3 code lines we added):

diff --git a/src/Orchard/ContentManagement/DefaultContentManager.cs b/src/Orchard/ContentManagement/DefaultContentManager.cs
index 6cdfed3..56f6eb4 100644
--- a/src/Orchard/ContentManagement/DefaultContentManager.cs
+++ b/src/Orchard/ContentManagement/DefaultContentManager.cs
@@ -703,6 +703,12 @@ namespace Orchard.ContentManagement {

         var context = new ImportContentContext(item, element, importContentSession);
  • // --adenin: added
  • var updateContext = new UpdateContentContext(item);
  • Handlers.Invoke(handler => handler.Updating(updateContext), Logger);
  • // --
  •      foreach (var contentHandler in Handlers) {
    @@ -711,6 +717,8 @@ namespace Orchard.ContentManagement {
         var savedItem = Get(item.Id, VersionOptions.Latest);

         // the item has been pre-created in the first pass of the import, create it in db
@@ -723,6 +731,10 @@ namespace Orchard.ContentManagement {
  • // --adenin: added
  • Handlers.Invoke(handler => handler.Updated(updateContext), Logger);
  • // --
         if (status == null || status.Value == Published) {