Sitecore - Unicorn setup, patterns and tips.

Sitecore - Unicorn setup, patterns and tips.

There are a few options to manage items in the Sitecore, Of course, TDS was one of the best during the initial Sitecore release. Now (In my opinion) Unicorn took place and has all the required functionality and feature to manage the items in Sitecore and integrate with the CI/CD pipelines.

Here we will discuss the basic setup for Unicorn considering the Haylix principal, Multisite and a few tips for the consideration like using the patterns to exclude a user-defined folder and a few handy free tools for the troubleshooting.

Below is the default configuration structure - As per Haylix principal,

/sitecore/templates/[Project|Feature|Foundation]/[Module]
/sitecore/layout/renderings/[Project|Feature|Foundation]/[Module]
/sitecore/layout/layouts/[Project|Feature|Foundation]/[Module]
/sitecore/layout/placeholder settings/[Project|Feature|Foundation]/[Module]

Example configuration from the Unicorn - Base abstract configuration -

<configuration xmlns:patch="https://www.sitecore.net/xmlconfig/">
    <sitecore>
        <unicorn>
            <configurations>
                <configuration name="Foundation.Serialization.Base" abstract="true">
                    <targetDataStore physicalRootPath="$(sourceFolder)\$(layer)\$(module)\serialization" />
                    <predicate type="Unicorn.Predicates.SerializationPresetPredicate, Unicorn" singleInstance="true">
                    </predicate>
                    <syncConfiguration type="Unicorn.Loader.DefaultSyncConfiguration, Unicorn" singleInstance="true" updateLinkDatabase="false" updateSearchIndex="true" maxConcurrency="1" />
                </configuration>
            </configurations>
        </unicorn>
    </sitecore>
</configuration>

Minimize the configuration in other modules, e.g. the Feature/Basic Content module:

<configuration xmlns:patch="https://www.sitecore.net/xmlconfig/">
    <sitecore>
        <unicorn>
            <configurations>
                <configuration name="Feature.BasicContent" extends="Foundation.Serialization.Base" description="BasicContent definition items" dependencies="Foundation.*" patch:after="configuration[@name='Foundation.Serialization.Base']">
                    <predicate>
                        <include name="templates" database="master" path="/sitecore/templates/Feature/BasicContent" />
                        <include name="renderings" database="master" path="/sitecore/layout/renderings/Feature/BasicContent" />
                        <include name="buttons" database="core" path="/sitecore/content/Applications/WebEdit/Custom Experience Buttons/BasicContent" />
                    </predicate>
                </configuration>
            </configurations>
        </unicorn>
    </sitecore>
</configuration>

For all test configuration, check Here

Below are some key consideration and tips.

  1. If you want to exclude all folder name like "User Content", you can use the pattern.
<exclude namePattern="^User Content$" />


  1. Define configuration based on the role so you can manage the developer machine or developer environment with all the content and higher environment you can push only required content like template, layout, rendering, shared data, configuration settings etc.
  2. Define the base configuration in the foundation layer, As an example below.
  3. Define the role very carefully and based on the actual environment need.
 <sitecore role:require="Standalone or ContentManagement">

Publishing part can be configured in the Unicorn.AutoPublish.config, Example below

		<unicornSyncEnd>
				<!-- when all configurations have synced, fire off a publish that processes the queue we've accumulated -->
				<processor type="Unicorn.Pipelines.UnicornSyncEnd.TriggerAutoPublishSyncedItems, Unicorn">
					<PublishTriggerItemId>/sitecore/templates/Common/Folder</PublishTriggerItemId> <!-- the trigger item can be any leaf node Sitecore item - just has to have a 'starting point' for the publish -->
					<!-- these are the database(s) to publish synced items to -->
					<TargetDatabases hint="list:AddTargetDatabase">
						<web>web</web>
					</TargetDatabases>
				</processor>
			</unicornSyncEnd>

Steps to setup the Unicorn - Considering multisite.

  1. First, define the folder location. (Foundation Layer) , Example path - C:\src
<configuration xmlns:patch="https://www.sitecore.net/xmlconfig/" xmlns:role="https://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="Standalone or ContentManagement">
    <sc.variable name="sourceFolder" value="C:\src" />
    <settings>
      <!--  Rainbow - SERIALIZATION FOLDER PATH MAX LENGTH
		      In Windows, there is 248 characters limit on the lenght of file system paths. To avoid exceeding the maximum path length, Rainbow will loop
				      long paths back to the root. This setting specifies the maximum length of the path to the serialization root path,
				      which determines how long item paths can be before they are looped.
		      Important: The value of this setting must be the same on all Sitecore instances accessing the serialized data. 
		      Important: When changing this value, you must reserialize all configurations!
		      Example: A value of "90" for this setting will mean that item paths longer than 150 characters will be shortened, since Sitecore 
		      reserves 8 characters (and 248 - 8 - 90 = 150). 
		      Default value: 90
		    -->


      <setting name="Rainbow.SFS.SerializationFolderPathMaxLength" value="190" />
      <setting name="Rainbow.SFS.MaxItemNameLengthBeforeTruncation" value="10" />
    </settings>
  </sitecore>
</configuration>

2. Define the base configuration in the (foundation layer), Example below

<configuration xmlns:patch="https://www.sitecore.net/xmlconfig/" xmlns:role="https://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="Standalone or ContentManagement">
    <unicorn>
      <configurations>
        <configuration name="SampleProject.Base" abstract="true">
          <predicate type="Unicorn.Predicates.SerializationPresetPredicate, Unicorn" singleInstance="true" />
          
          <targetDataStore physicalRootPath="$(sourceFolder)\$(layer)\$(module)\serialization" useDataCache="false" singleInstance="true" />
          <roleDataStore type="Unicorn.Roles.Data.FilesystemRoleDataStore, Unicorn.Roles" physicalRootPath="$(sourceFolder)\$(layer)\$(module)\serialization\Roles" singleInstance="true" />
          <rolePredicate type="Unicorn.Roles.RolePredicates.ConfigurationRolePredicate, Unicorn.Roles" singleInstance="true">
            <!-- Include an invalid predicate or all roles will be synced -->
            <include domain="invaliddomain" pattern="none" />
          </rolePredicate>
        </configuration>
      </configurations>
    </unicorn>
  </sitecore>
</configuration>

3. Shared configuration in Shared project - I would suggest keeping two separate files, One for Standalone or content management and second for Standalone.

<configuration xmlns:patch="https://www.sitecore.net/xmlconfig/" xmlns:set="https://www.sitecore.net/xmlconfig/set/" xmlns:role="https://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="Standalone">
    <unicorn>
    </unicorn>
  </sitecore>
</configuration>

Troubleshooting - If you are packaging and installing Sitecore items, You may see below errors - You can get the BasicCompany setup from the Sitecore here - https://github.com/Sitecore/Helix.Examples

No alt text provided for this image

to fix that basically, You need to install the required package and deserialize those items and that will generate the yml files.

No alt text provided for this image


Perform Initial Serialization for a new project or module.

No alt text provided for this image


If you see this error, It means you have recently included a new configuration, As an example below.

No alt text provided for this image

to fix that, basically, you have to go to the end of the unicorn page and to the initial setup, that will generate the required yml file.

No alt text provided for this image

Tips - Use a free tool AgentRanSack - You can download from here - https://www.mythicsoft.com/agentransack/download/ This tool is very handy to resolve serialization issues like duplicate items, cleaning a few unwanted items and so. the lite version is absolutely free.

No alt text provided for this image

Example to search files, you can right-click on the file and delete it or can perform another search

No alt text provided for this image

Tips - This is a popular point of discussion on how to sync the content between the local development environment and higher environment like Development, QA, Staging, UAT and Production.

For that, We can define the separate configuration based on the role, Like for the developer machine (Standalone) mode, We would need all pages or data sources, but for the higher environment, we may not need to push/update those items. So Role-based configuration is the most critical part, so don't forget it when you add your next module. Or predicates.

I hope this will be helpful to all my Sitecore friends.

Manu Abhishek Dwivedi

Lead - Azure AWS Sitecore .Net Microservices

4 年

Is there any way to delete parent folder with unicorn ? For example if I have renamed a folder on Development and now I am doing sync on Test environment then my previous folder is also present as I have removed mapping in config.

要查看或添加评论,请登录

Jitendra Soni的更多文章

社区洞察

其他会员也浏览了