Best practices when creating a custom PublishPostProcess plugin
Best practices when creating a new publish plugin lets you get the best out of your customization.
Implementation plan
- Choose the category of the plugin. For example, if you want the plugin to be triggered after the files are exported to the file system, you need to create a plugin that implements the
IPublishPostProcessinterface. - Choose the interface to implement. When writing a new custom plugin, you should implement one of the available interfaces:
- Implement the
IPublishPostProcesswhen your plugin needs to be called after the files are exported to the file system. This is currently the only supported Publish plugin.
- Implement the
- Implement
Initialize. Here the plugin should read its parameters and parse their values. In case a value is invalid, the plugin sequence can fail early in the process, before any plugin is executed. - Implement
Run. Here you put the main logic of the plugin. Anything that plugin can do is available to it through the context, which includes:- The object that was used to start the publish
- Information of where the content and metadata files were exported, so they can be accessed
- The condition set used
- Output format fields
- Whether the publish needs to compare versions
- Whether the publish needs to combine multiple languages into one output result
- Logging (Use the
LogServiceproperty of the context to add the logging statement to the standard application log file)
- Expose the plugin using the Export attribute to make sure that the plugin engine can discover the plugin.
General best practices
-
Pass
falseto any API 2.5 constructor - Make sure to pass one parameter, set to
false, to any API 2.5 constructors you use in your plugin. Without this parameter included and set tofalse, you get a security error. This parameter,initializeCallContext, when set tofalse, causes the call context to be reused, effectively keeping your code within the same process. -
Verify the parameter values in
Initialize - If the plugin cannot be executed because some parameters were not provided or have wrong values, the main operation should fail as soon as possible.
-
Use
IshValueType.elementfor getting and setting field values - This ensures that the element name does not change and is guaranteed to be unique.
-
Use the items property on the
IPublishPostProcessContext -
The
Itemsproperty on theIPublishPostProcessContextcan be used to pass data in memory between plugins. However make sure not to let too much data loaded in memory: This can cause the server to run out of memory. - Don't log the processing of every file
- Logging every file that is being processed in the event monitor generates too many entries in the Event Log. We recommend you only periodically log the number of files already processed in such a way that it would lead to 10 lines in the event monitor. On the other hand, if the plugin encounters and error, make sure to log the exact file that caused the error.
- Think multithread
- If your process takes a long time, you might want to use multiple threads: it will improve throughput.
- Check cancellation
- Check that the publish was not cancelled on a regular basis.