The following sample code demonstrates how this can be done. The CloneCountry method in the example creates a clone of the USA country object, including all child states. It also manually sets the country code values of the new object.
Before an object can be cloned, it is necessary to prepare an instance of the CloneSettings class (from the CMS.SettingsProvider namespace). By assigning values to the properties of this object, you can configure how the cloning process will be performed. In addition to the code name and display name, the available properties correspond with the advanced cloning settings. In the example, the IncludeChildren flag is set to true, so all state objects under the given country will also be cloned and assigned to the new country.
The BeforeCloneInsertCallback property is used to register a custom handler method that will be executed just before the clone is inserted into the database. Such methods allow you to implement any functionality required to correctly clone the object. The object that is being cloned and the corresponding CloneSettings object are passed as parameters, so you can dynamically set the values based on the currently assigned clone settings.
Other possible callback options are:
•AfterCloneInsertCallback - executed after the cloned object itself is created and inserted, but before the system starts cloning any associated child objects or bindings.
•AfterCloneStructureInsertCallback - called once all objects included in the cloning process are created.
Finally, the clone action itself is performed by calling the InsertAsClone method for the original country object (converted to a generalized object type), with the prepared CloneSettings specified through the parameter.