Is this really the version you are looking for? Kentico Xperience 10 Documentation is no longer updated. Use the switcher in the header to choose your product version.
Cloning allows you to create exact copies of objects in the system. All objects support cloning, from the most basic records to complex data structures such as page types or user accounts.
Manually cloning objects
To clone an object in the Kentico administration interface:
You can clone object by calling the appropriate API in your custom code, for example in:
The following sample code demonstrates how to clone objects using the API. The CloneCountry method in the example:
using CMS.Globalization;
using CMS.DataEngine;
...
public void CloneCountry()
{
// Gets the country to be cloned.
CountryInfo country = CountryInfoProvider.GetCountryInfo("USA");
if (country != null)
{
// Prepares the settings used for the cloning process.
CloneSettings settings = new CloneSettings()
{
// Sets the clone names.
CodeName = "MyClonedCountry",
DisplayName = "My Cloned Country",
// Ensures that the state objects under the country are also cloned.
IncludeChildren = true,
// Registers a callback method that performs additional actions before the clone is added.
BeforeCloneInsertCallback = ChangeCountryCodes
};
// Clones the country according to the defined settings.
country.Generalized.InsertAsClone(settings);
}
}
private void ChangeCountryCodes(CloneSettings settings, BaseInfo cloneToBeInserted)
{
// Changes the values of additional fields before the clone is inserted to the DB.
CountryInfo country = cloneToBeInserted as CountryInfo;
if (country != null)
{
country.CountryThreeLetterCode = "MCC";
country.CountryTwoLetterCode = "MC";
}
}To clone an object, you need to:
CloneSettings properties
In addition to setting the code name and display name of the cloned object, the properties of the CloneSettings class determine what operations the system performs during the cloning process. In the example, the IncludeChildren flag is set to true, so the code also clones all state objects under the given country and assigns them to the new country.
The BeforeCloneInsertCallback property registers a custom handler method that the system executes 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: