# Migrate your project

## Migration commands

You use the migration commands to migrate frontend resources to a decoupled renderer architecture based on ASP.NET Core or Next.js.

These include page templates, pages, built-in widgets, forms, and form responses - written in Web Forms or MVC. You use Sitefinity CLI to migrate a top-level resource – a page template or a page. Afterwards, the CLI automatically migrates the resources used on it, such as widgets, and forms.

Sitefinity CLI has the following commands:

* `sf migrate page "PageId"`\
  Migrates a page.
* `sf migrate template "TemplateId"`\
  Migrates a page template.
* `sf migrate responses "FormId"`\
  Migrates the form responses for the form with the specified FormId.
* `sf migrate --help`\
  Prints online the same help as this article.

> info **NOTE**: The `PageId`, `TemplateId`, and `FormId` are the internal GUIDs that Sitefinity CMS uses to identify the respective frontend resource. You can use the Sitefinity Migration Analyzer to conveniently get these GUIDs. For more information, see [Sitefinity Migration Analyzer](https://www.progress.com/documentation/sitefinity-cms/sitefinity-migration-analyzer).

> important **IMPORTANT**: The Sitefinity CLI tool helps you migrate only the following frontend resources:
>
> * the content and structure of the page templates and pages.
> * built-in widgets, if they have equivalent in the decoupled renderers.
>   For more information, see [Widgets](https://www.progress.com/documentation/sitefinity-cms/widgets-site-components).
> * form responses
>
> You still need to re-implement all custom widgets you are using on your site.
> The migration tool is not a complete solution and can generate warnings or incomplete front-end resources. You are responsible to check its results.

> important **IMPORTANT**: The `migrate` command supports migration from Web Forms and MVC widgets to ASP.NET Core and Next.js widgets only.

> info **PREREQUISITES**: The migration commands support only projects based on Sitefinity 15.3 and higher.\
> If you want to migrate a project based on an earlier Sitefinity CMS version, you need to first upgrade your project to at least Sitefinity 15.3.

## General flow of migration

> info **RECOMMENDATION**: We recommend analyzing and evaluating the state of your Sitefinity project and estimating the resources needed for migration before starting the migration itself.\
> For more information, see [Technology migration](https://www.progress.com/documentation/sitefinity-cms/technology-migration).

To migrate your Sitefinity CMS project, perform the following procedure:

1. Migrate the templates.\
   Start with the migration of templates that a subset of pages is based on OR migrate all of the page templates at once.
2. Adjust the migrated structure as needed.
   * Set a file system template.
   * Migrate the widgets used on the template.
     * Take a business decision whether you can stop using some of the existing widgets. For example, remove existing widgets if the business need is no longer there.
     * Manually configure the widgets that are migrated.
   * Adjust the look and feel of the page.
3. Once you complete the migration of the chosen page templates, move on to the pages structure.
   * Sitefinity CLI duplicates the Pages by default and excludes the copies from the navigation.
   * Once you fully migrate the page, specify the `--replace` option to the CLI `migrate page` command.
4. Migrate the forms.\
   For each form you are using, perform the following:
   * Complete the migration of all pages and page templates where you are using this form in one step.
   * Ensure that only the migrated form is used on the frontend and the initial form is hidden from all MVC and Web Forms resources.
   * Run the CLI command `migrate responses` to migrate form responses.

     > important **IMPORTANT:** Sitefinity CLI migrates fully all form responses every time you run the command. Therefore, if you run the command multiple times, Sitefinity CLI will create duplicated form responses.\
     > To avoid duplications, delete the common responses before running the `migrate responses` command for second time.

### Considerations

* You can specify the `--replace` option only when the page has a duplicate.
* The CLI uses the page's `UrlName` property OR the page template's `Name` property to identify the page or the page template that it created. The format has a suffix of `(migrated to Decoupled)`. This process avoids conflicts with existing pages and page templates.
* The CLI uses only data from the published pages and page templates. `Draft` and `Temp` changes are not migrated.
* Sitefinity CLI automatically migrates forms, if there is a form widget on the page.

## Migrating hierarchies

* When you select a page template, the CLI migrates first the parent page templates. Migration cannot happen otherwise.
* If parent page templates are migrated automatically, they will be published automatically.

> info **NOTE**: When migrating pages, the CLI will not migrate the parent page templates. You must migrate all dependent page templates before migrating pages.

## Safe box & Testing

All pages and page templates are duplicated by default with a suffix `(migrated to Decoupled)` appended to the their `Title`. This provides a level of isolation for existing pages and page templates, so that the migration can happen seamlessly and without downtime. This is a useful way to test the changes before they go live.

> info **NOTE**: When migrating pages, you can use the option `--replace`. This option replaces the page contents on the **actual** page and saves the page automatically as a `Draft`, regardless of the value of the `--action` option.\
> This option keeps the existing links from content blocks, HTML fields, and related data; thus, you do not need to update these references.

> info **NOTE**: Duplicated Pages are hidden from navigation by default.

## CLI command-line parameters

### Required parameters

The following CLI parameters are required.

* `--cmsUrl`\
  The URL of the deployed Sitefinity CMS.\
  For example, `https://www.example.com/en`
* `--token`\
  The authentication token to use.\
  To learn how to generate a token, see [Generate access key](https://www.progress.com/documentation/sitefinity-cms/generate-access-key). You must use a token of an account with full *Administrator* privileges.

### Optional parameters

The following CLI parameters are optional:

* `--framework`\
  Specifies the target renderer framework. Valid only for page templates and forms. The accepted values are `NetCore` for ASP.NET Core and `NextJS` for Next.js. If you omit this option, the default ASP.NET Core renderer is used.
* `--recreate`\
  Recreates the selected page or template **and** its parent templates.\
  Useful when testing and experimenting with custom configurations and custom widget migrations.
* `--recursive`\
  Recursively migrates all the child pages or templates of the selected page or page template. When migrating page templates, the tool does not recursively migrate pages.
* `--replace`\
  Replaces the content of the page. Valid only for pages.
* `--action`\
  The action to execute at the end of the migration. Allowed values are:
  * `draft` - saves the migrated resource as `Draft`.
  * `publish` - publishes the migrated resource.
* `--siteid`\
  The site id. You use the `--siteid` parameter to specify the site id when you work with a non-default site.

> info **NOTE**: You can set the parameters manually in the appsettings.json file. You need to manually create the appsettings.json file next to the *sf.exe* binary.\
> You can mix both appsettings.json parameters and direct command-line parameters, with the latter having precedence.

> success **EXAMPLE**: Following is an sample appsettings.json file.
>
> ```JSON
> {
>  "Commands": {
>    "Migrate": {
>      "CmsUrl": "http://localhost",
>      "Token": "TOKEN",
>      "Recreate": false,
>      "Recursive": false,
>      "Replace": false,
>      "Action": "publish",
>      "SiteId": "00000000-0000-0000-0000-000000000000",
>      "Widgets": {
>          "Telerik.Sitefinity.Modules.GenericContent.Web.UI.ContentBlock": {
>              "Name": "SitefinityContentBlock",
>              "Whitelist": ["Html", "ProviderName", "SharedContentID"],
>              "Rename": {
>                "Html": "Content"
>              },
>              "CleanNullProperties": true
>          }
>      }
>    }
>  }
> }
> ```

## Widget migration

You can migrate widgets in two ways - using configuration and using custom widget migrations.

### Migration through configuration

You specify the migrations in the appsettings.json file.

> success **EXAMPLE**: You specify the migrations in the appsettings.json file in the following way:
>
> ```JSON
> {
>  "Commands": {
>    "Migrate": {
>      ...
>      "Widgets": {
>          "Telerik.Sitefinity.Modules.GenericContent.Web.UI.ContentBlock": {
>              // the name of the new widget in ASP.NET Core and Next.js renderers
>              "Name": "SitefinityContentBlock",
>              // the whitelist of properties to keep during the migration
>              "Whitelist": ["Html", "ProviderName", "SharedContentID"],
>              "Rename": { // the properties to be renamed
>                "Html": "Content"
>              },
>              "CleanNullProperties": true
>          }
>      }
>    }
>  }
> }
> ```

### Custom widget migrations

You can perform custom widget migrations when the configuration using the CLI is insufficient and more complex logic is required. You use C# code to extend the functionality of the CLI itself.\
You can reference the built-in widget migrations, located under the `Migrations` folder in the CLI source code.

Widget migrations are invoked for each **occurrence** of the widget found on the page or page template. For each invocation, a `WidgetMigrationContext` is passed. It contains the following:

* `SourceClient`\
  The `IRestClient` for interfacing with the CMS.
* `Source`\
  The original widget, from which you are migrating.
* `ParentId`\
  The new parent id for the migrated widget.
* `Language`\
  The current language.
* `SegmentId`\
  The page segment when working with personalized pages.
* `WidgetSegmentId`\
  The widget segment when working with personalized widgets.
* `LogWarning`\
  Specifies a warning message to be shown in the Terminal during the migration of the widget.
* `LogInfo`\
  Specifies an informational message to be shown in the Terminal during the migration of the widget.
* `SiteId`\
  The site id. You use the `SiteId` parameter to specify the site id when you work with a non-default site.

A return value of type `MigratedWidget` is required as the output of the migration. It contains the new widget name and properties.

### Migration of complex properties

In complex projects, your business logic may use complex properties. For example:

```C#
// in your MVC controller
public MyComplexModel Model {get; set;}

public class MyComplexModel {
  public MyEntity Entity {get; set;}
}

public class MyEntity
{
  public string Title {get;set;}
}
```

When migrating such complex logic, you need to fork [Sitefinity CLI](https://github.com/Sitefinity/Sitefinity-CLI) and manually add the migration logic to it, as described in *Custom widget migrations* above.

Your `Migrate` method can access the complex properties using the `context.Source.Properties` property of its `context` parameter, as described above.\
You form the key to access a property by separating the nested property names with dashes. In the example above, the key is `Model-Entity-Title`.

> success **EXAMPLE**: For example about migrating widgets with complex properties in the controller, see the `Migrate` method in [SearchBoxWidget.cs](https://github.com/Sitefinity/Sitefinity-CLI/blob/master/Sitefinity%20CLI/Migrations/Mvc/SearchBoxWidget.cs).

If you are migrating properties from the same type, a simpler approach is to use JSON config mappings, as described in the *Migration through configuration* section above.

> success **EXAMPLE**: For an example of how to use JSON config mappings, see [WidgetMigrationDefaults.cs](https://github.com/Sitefinity/Sitefinity-CLI/blob/master/Sitefinity%20CLI/Migrations/WidgetMigrationDefaults.cs).

### Useful functions

From base class `MigrationBase:`

* `ProcessProperties`\
  Copies and renames properties.
* `GetMasterIds`\
  Gets the master ids of the live content items (usually referenced in Web Forms widgets).
* `GetSingleItemMixedContentValue`, `GetMixedContentValue`\
  Helper for generating properties of type `MixedContentContext`.

## Map form fields when migrating form responses

When migrating forms responses, you may want to change the type of fields you are migrating.

For example, you may want to migrate a custom field to a built-in one, or migrate a built-in one, which is no longer supported, to a custom one.

To do this, you create a mapping configuration in the `appsettings.json` configuration file of sf.exe.

In the `FormFieldNameMap` object, you create a name/value pair, containing the names of the developer types of the respective form fields.

For example, to map a MVC text form field to an ASP.NET Core form field, the configuration should look like:

```JSON
"FormFieldNameMap": {
 "sf_contactus.TextFieldController": "SitefinityTextFieldFormControl"
}
```

You construct the JSON key using the name of the source form (the one you migrate from), DOT, and the name of the form field. The JSON value is the name of the target form field (the one you migrate to).

In the Migration Analyzer, you navigate to the form you want to migrate. You take the form name from the `'Form name' form info` table, under the `Form name` column, and the field name from the `Fields used in this form` table under the `Name (used in code)` column.

You can take both names from the Sitefinity Migration Analyzer. For more information, see [Sitefinity Migration Analyzer Forms](https://www.progress.com/documentation/sitefinity-cms/sitefinity-migration-analyzer-forms).

> success **EXAMPLE**:
>
> ```JSON
> {
>  "Commands": {
>    "Migrate": {
>      "CmsUrl": "https://yoursitefinityinstance.net",
>      "Token": "authentication-token",
>      "PlaceholderMap": {
>        "Contentplaceholder1": "Body"
>      },
>      "Widgets": {
>        "Telerik.Sitefinity.Modules.GenericContent.Web.UI.ContentBlock": {
>          "Name": "SitefinityContentBlock", // the name of the new widget in the ASP.NET Core or Next.js renderers
>          "Whitelist": [ "Html", "ProviderName", "SharedContentID" ], // the whitelist of properties to keep during the migration
>          "Rename": { // the properties to be renamed
>            "Html": "Content"
>          },
>          "CleanNullProperties": true
>        },
>        "Telerik.Sitefinity.Frontend.Forms.Mvc.Controllers.CaptchaController": {
>          "Name": "Captcha2", // the name of the new widget in the ASP.NET Core or Next.js renderers
>          "Whitelist": [], // the whitelist of properties to keep during the migration
>          "Rename": {
>          },
>          "CleanNullProperties": true
>        }
>      },
>      "FormFieldNameMap": {
>          "sf_contactus.TextFieldController": "SitefinityTextFieldFormControl"
>      }
>    }
>  }
> }
> ```

## Limitations

The CLI migration command does not migrate your custom code in any form. It can migrate only the content and structure of your project.

### Migrating List widget to Content list widget

When you migrate a List widget ([Web Forms](https://www.progress.com/documentation/sitefinity-cms/133/list-widget-webforms), [MVC](https://www.progress.com/documentation/sitefinity-cms/list-widget-mvc)) to a Content list widget ([ASP.NET Core](https://www.progress.com/documentation/sitefinity-cms/content-list-widget-core), [Next.js](https://www.progress.com/documentation/sitefinity-cms/next.js-content-list-widget)) and the existing widget shows two separate lists, the new Content list widget will show a single view, containing merged items from all original lists.

To show the multiple original lists separately, perform one of the following:

* Create a new view to perform custom sorting and iterate the lists separately.
* Use multiple separate Content List widgets, each configured to display only one of the lists.

### Migrating Web Forms Image widget to Image widget

When you migrate a [Web Forms Image widget](https://www.progress.com/documentation/sitefinity-cms/133/image-widget-webforms) with display mode set to `Custom`, the custom display setting is not migrated.

To work around, manually configure the image size through the widget designer.

### Migrating Dynamic widget to Content list widget

When migrating a [Dynamic content widget](https://www.progress.com/documentation/sitefinity-cms/dynamic-content-widgets) to a Content list widget ([ASP.NET Core](https://www.progress.com/documentation/sitefinity-cms/content-list-widget-core), [Next.js](https://www.progress.com/documentation/sitefinity-cms/next.js-content-list-widget)) and the main field is changed to a value different from the one in the `Title`field, you need to perform the following:

1. Manually change the list mapping in the widget designer.
2. Update the mapping for the `Details` view.\
   For ASP.NET Core, you use the `ViewsMetadata.json` file.\
   For more information, see [Create custom views for the Content list widget](https://www.progress.com/documentation/sitefinity-cms/create-custom-views-for-the-content-list-widget) » *Field Mappings*.\
   For Next.js, you modify the `ListFieldMapping` property. For more information, see [Extend the Content list widget](https://www.progress.com/documentation/sitefinity-cms/next.js-extend-content-list-widget).

### Migrating Blogs to Content list

Sitefinity CLI migration command does not support migrating Blogs widget ([Web Forms](https://www.progress.com/documentation/sitefinity-cms/133/blogs-list-widget-webforms), [MVC](https://www.progress.com/documentation/sitefinity-cms/blogs-widget-mvc)).

You need to implement a custom widget in ASP.NET Core and manually migrate using this new widget. We recommend using the Content List widget ([ASP.NET Core](https://www.progress.com/documentation/sitefinity-cms/content-list-widget-core), [Next.js](https://www.progress.com/documentation/sitefinity-cms/next.js-content-list-widget)) to implement your custom widget.

### Migrate Events widget to Content list widget

The new Content list widget ([ASP.NET Core](https://www.progress.com/documentation/sitefinity-cms/content-list-widget-core), [Next.js](https://www.progress.com/documentation/sitefinity-cms/next.js-content-list-widget)) comes with different predefined basic filter options than the MVC and Web Forms widgets.

Migration process transfers the old filters for past, current, and upcoming events into the *Filter Expression* field in the Advanced mode of the widget designer.

Because of the difference in the filtering functionality between the different widgets, after the migration, you need to check the filters directly in the *Filter Expression* field.

If the filters expressions cannot be matched, you need to implement a custom widget in ASP.NET Core and manually migrate using this new widget. We recommend using the Content List widget ([ASP.NET Core](https://www.progress.com/documentation/sitefinity-cms/content-list-widget-core), [Next.js](https://www.progress.com/documentation/sitefinity-cms/next.js-content-list-widget)) to implement your custom widget.

### Migrate Calendar widget

Sitefinity CLI cannot migrate the Calendar widget.

You need to create a custom widget, based on Content List widget ([ASP.NET Core](https://www.progress.com/documentation/sitefinity-cms/content-list-widget-core), [Next.js](https://www.progress.com/documentation/sitefinity-cms/next.js-content-list-widget)) to implement your custom widget.

### Migrate Login form with Reset password mode set to Reset password widget

If you have configured the Login form widget ([Web Forms](https://www.progress.com/documentation/sitefinity-cms/133/login-widget-webforms), [MVC](https://www.progress.com/documentation/sitefinity-cms/login-form-widget-mvc)) with *Allow users to reset password* to ASP.NET Core renderer, it is migrated without the option to reset the password because there is a separate widget providing that functionality.

You need to manually add the new Reset password widget ([ASP.NET Core](https://www.progress.com/documentation/sitefinity-cms/reset-password-widget), [Next.js](https://www.progress.com/documentation/sitefinity-cms/nextjs-reset-password-widget)) on the page and configure it.

### Replacement of multilingual pages

The following scenario is not supported:

1. You migrate a page
2. You add new languages only to the migrated copy of the page
3. You attempt to migrate the page again, using the `--replace` option

In this scenario, only the single original language of the original page is preserved.

### Migrating form responses

Sitefinity CLI gives you the flexibility to work on the original and migrated pages at the same time.\
If the page that you are migrating has a form, and you migrate the form responses while you have the same form on the two variants of the page (using the original and the target frameworks), then Sitefinity CLI will duplicate all form responses for the old and new form.\
To avoid duplications, delete the entries from the old form before you run the migrate responses command for a second time.

> info **RECOMMENDATION:** We recommend that you complete the page (or page template) migration first, hide the original form, based on MVC or Web Forms; and only then migrate the form responses for the forms on that page (or page template).
