1

I have a Web API service that I have managed to use to pull values into a ContentPart by using the method outlined here: How to change Orchard record repository.

Now, what I want to do is update the values of the properties on my ContentPart and have the update performed via a Web API call. I think I'm close, but just can't get an update method to fire.

In my Handler, I have added a handler for OnUpdate. I can see my OnUpdate method get registered, but it never executes.

So my question is: Is this the correct handler event to use? And if so, how can I get it to trigger?

I should add that I am accessing the ContentPart through a Controller, as opposed to a Driver.

Community
  • 1
  • 1
Chris Payne
  • 1,700
  • 1
  • 16
  • 33
  • Why not call into the web service from the controller action? Also, even if you have a special controller, your part should have a driver. – Bertrand Le Roy Apr 12 '13 at 21:47
  • We started out calling the web services from the controller, but as the product got larger, the actions on the controllers started to get hard to read/ manage. Creating parts is part of a piece of work to abstract access to common items we require. I know there are other ways to do this, but I saw this as an opportunity to learn more about Orchard, and how parts work. – Chris Payne Apr 13 '13 at 12:03
  • When I say do it from the controller, I don't mean all the logic should be there of course: controller actions should always remain lean. I meant initiate them from the action, but put the logic in a service class. – Bertrand Le Roy Apr 14 '13 at 04:37

1 Answers1

1

OnUpdating / OnUpdated get fired whenever you call IContentManager.UpdateEditor(item). In the default scenario this happens when you hit "Save" button when editing your content item.

I don't quite get what you mean by "accessing the ContentPart through a Controller"?

Do you have a custom controller that handles the item editor rendering and it's postback? Or are you creating and updating some items in code, without using the built-in editors at all?

  • In the former case, you need to ensure that IContentEditor.UpdateEditor(item) gets called for the whole content item inside the POST action (same way it does in the default controller - Core\Contents\Controllers\AdminController.cs).
  • In the latter, which I guess might be the case here, OnUpdating / OnUpdated won't be fired and you need to call the web service on your own from the controller action, as Bertrand pointed out in the comments.

There is also a third option available and I found it particularly useful in similar cases:

  1. Use LazyField<T> as a backing field for those part properties you need to push to web service after update.
  2. Put the code that calls web service inside that lazy field's setter (set this up during handler OnActivated event).
  3. Now, whenever your property gets updated, a call to web service will be made, ie. the lazy field will act a a transparent proxy between your web service and current code.

For examples how to work with lazy fields take a look into CommonPartHandler class and methods LazyLoadHandlers and PropertySetHandlers.

Piotr Szmyd
  • 13,371
  • 6
  • 44
  • 61