Exclude Fields from Serialization in Sitecore CLI


Today, I am going to explain the steps that are required to exclude some of the fields from getting serialized from Sitecore CLI.

Sitecore CLI by default includes all the fields during serialization but to reduce the size of the serialized file or to get rid of unnecessary fields you can exclude the fields by specifying fields to exclude from serialization by configuring excluded fields in the sitecore.json or modules.json files.

One thing to note is that excluding fields from serialization is available from CLI version 4.0 or above.


The excludedFields property in the JSON file consists of an array of the excluded fields having two properties: fieldId and description.

Another thing to note is excluding the Revision field from the Sitecore serialization can result in an issue when trying to publish with the serialization command (dotnet sitecore publish).

Sync operations (push and pull) in Sitecore CLI always ignore the excluded fields.

If you change the excluded fields in Sitecore even then the Sitecore CLI does not detect any changes, and pushing data does not override these fields.

One more thing to note is if you have added excluded fields in the .module.json or sitecore.json file later then the yml files will still contain the excluded fields. Then the excluded fields are removed from the yml files only after having some changes in the included fields or if you manually delete the yml files and then perform a pull operation.

Setting up the excluded fields in the sitecore.json file

By default, the excludedFields array is empty in the sitecore.json file.


You can specify the fields you want to exclude from Sitecore serialization in the excludedFields array.


{
  "$schema": "./.sitecore/schemas/RootConfigurationFile.schema.json",
  "modules": [
    "src/*/*/*.module.json"
  ],
  "plugins": [
    "Sitecore.DevEx.Extensibility.Serialization@4.1.0",
    "Sitecore.DevEx.Extensibility.Publishing@4.1.0",
    "Sitecore.DevEx.Extensibility.Indexing@4.1.0",
    "Sitecore.DevEx.Extensibility.ResourcePackage@4.1.0"
  ],
  "serialization": {
    "defaultMaxRelativeItemPathLength": 100,
    "defaultModuleRelativeSerializationPath": "serialization",
    "removeOrphansForRoles": true,
    "continueOnItemFailure": false,
    "excludedFields": [
      {
        "fieldId": "badd9cf9-53e0-4d0c-bcc0-2d784c282f6a",
        "description": "__Updated by"
      },
      {
        "fieldId": "5dd74568-4d4b-44c1-b513-0af5f4cda34f",
        "description": "__Created by"
      },
      {
        "fieldId": "{52807595-0F8F-4B20-8D2A-CB71D28C6103}",
        "description": "__Owner"
      },
      {
        "fieldId": "{001DD393-96C5-490B-924A-B0F25CD9EFD8}",
        "description": "__Lock"
      }
    ]
  }
}

Below is the output of the above process of execution fields.


Setting up the excluded fields in the .module.json file

You can specify the fields you want to exclude from Sitecore serialization in the excludedFields array in the .module.json file.


{
	"namespace": "SitecoreItems",
	"references": "",
	"items": {
		"includes": [
		{
			"name": "Content",
			"path": "/sitecore/content"
		}],
		"excludedFields" : [
			{
				"fieldId": "d9cf14b1-fa16-4ba6-9288-e8a174d4d522",
				"description": "__Updated"
			},
			{
				"fieldId": "25bed78c-4957-4165-998a-ca1b52f67497",
				"description": "__Created"
			}
		]
	}
}

The benefit of excluding fields from .module.json is you can exclude fields for a particular module if needed.

Below is the output of the above process of execution fields.


Checking the excluded fields

After excluding fields from Sitecore serialization using any of the methods described above you can see the summary or complete information about the excluded fields using the below commands.

To check the summary of the configured excluded fields run the below command.

dotnet sitecore ser info


To check the full details of the configured excluded fields run the below command.

dotnet sitecore ser info -t


References


Chirag Goel

I am a developer, likes to work on different future technologies.

Post a Comment (0)
Previous Post Next Post