Excluding Specific Fields from Unicorn Serialisation (Field Filter)

unicorn

This week I had a situation where I needed to include some Sitecore Items in Unicorns Sync but exclude certain fields from those items.  In my case these were navigation items which had a link on them. I wanted to maintain the ids of the navigation item across environments but not the link – as it changes per environment.

I’ve used Unicorn a lot in quite a few Sitecore projects but I’ve never had to do this before so I wasn’t sure if it was possible or not.

It turns out it is, Unicorn has an feature called a ‘Field Filter’.

I can’t work out exactly when this became available but it looks like since version 4.0 Unicorn supports config based filtering.

Luckily we are on Unicorn 4.0.8 so we didn’t need to upgrade.

How do you use it?

Field filters are actually used in the <defaults> node in the Unicorn.config that is provided by default by Unicorn, here is an example of the Last run field being excluded:

<fieldFilter type=”Rainbow.Filtering.ConfigurationFieldFilter, Rainbow” singleInstance=”true”>
<exclude fieldID=”{B1E16562-F3F9-4DDD-84CA-6E099950ECC0}” note=”‘Last run’ field on Schedule template (used to register tasks)” />
</fieldFilter>

Adding the fieldFilter for a specific Configuration

Anything in Unicorn defaults can be overridden/defined at a configuration level  in my case this is what I wanted to do, here is how my custom configuration looks after adding in the exclusion rule for the Home Link field on my Navigation items:

After deploying this update and running Reserialize on this config I could see that the Home Link field had been excluded in the YAML file written to disk :-).

I couldn’t find much on this so thought this might help others who need to do this in future.

Further Options in Unicorn 4.1 – Field Transforms

After posting this Mark Cassidy (who works on Unicorn) tweeted me to remind me about a feature that has just been released and he blogged about it last week:

marks-tweet  

Essentially allowing you to keep certain fields under Source Control, but giving you much more control over what happens to the field values in specific environments using filters.

I haven’t tested this but it looks like this could be used like so to exclude my Home Link field:

In this case I’ve chosen to put the fieldTransforms on the <include> node but if you wanted to apply the filter to the whole config by adding it to the <predicate> instead.

When we’ve upgraded Unicorn to 4.1 I’ll test this and see how it works out.