Chris Payne
Chris Payne

Reputation: 1710

How to perform web service call after updating a content part in Orchard?

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.

Upvotes: 1

Views: 303

Answers (1)

Piotr Szmyd
Piotr Szmyd

Reputation: 13366

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.

Upvotes: 1

Related Questions