---
title: Cloning objects through the API
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

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 [document types](https://docs.kentico.com/k8/developing-websites/defining-website-data-structure/document-types.md) or [user accounts](https://docs.kentico.com/k8/managing-users.md).

> **Note:** **Manually cloning objects**
>
> To clone an object in the Kentico administration interface:
>
> 1. Find the object on the listing page in the appropriate section of the UI.
> 2. Click **...** next to the object and select **Clone**.
>    - For objects displayed in a tree menu instead of a standard list, use the clone button () in the panel above the tree.
> 3. Configure the clone settings and click **Clone**.

You can clone object by calling the appropriate API in your custom code, for example in:

- [Global event handlers](https://docs.kentico.com/k8/custom-development/handling-global-events.md)
- User controls or [web parts](https://docs.kentico.com/k8/custom-development/developing-web-parts.md)

The following sample code demonstrates how to clone objects using the API. The **CloneCountry** method in the example:

- Creates a clone of the USA country object, including all child states.
- Manually sets the country code values of the new object.

```csharp

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:

1. Prepare an instance of the **CloneSettings** class (requires a reference to the **CMS.DataEngine** namespace).
2. Configure the cloning process by assigning values to the properties of the CloneSettings object.
3. Call the **InsertAsClone** method for the original object (converted to a generalized object type), with the prepared _CloneSettings_ specified through the parameter.

> **Info:** **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:
>
> - **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.
