Serialization and deserialization with Sitecore Powershell Extensions

04 December 2013
Marek Musielak
Frink_Cognifide_2016_HeaderImages_0117

sitecore serialization and deserialization with cognifide powershell extensions

Every time one asks about automation of processes or tasks in Sitecore I hear one answer: 'Sitecore Powershell Extensions'. In most cases it's really easy to find a script which suits your need, so I was really surprised when I tried to find an information how to deserialize items with Powershell Extensions and I wasn't able to google anything helpful. I hope this post will fill the gap.

serialize-item

Serialization of items with Sitecore Powershell Extensions uses serialize-item command. The simplest command syntax is:
serialize-item -path "master:\content"
or
get-item "master:\content" | serialize-item
Both of them will serialize the content item in the master database. In first case we pass the path to the item as a parameter, in second case we serialize the items which come from the pipeline. You can send more items from the pipeline to the serialize-item command, e.g. if you need to serialize all the descendants of the home item created by sitecore\admin, you can use:
get-childitem "master:\content\home" -recurse | where-object { $_."__Created By" -eq "sitecore\admin" } | serialize-item
Other parameters of the serialize-item command are:
  • id - you can pass the id of serialized item instead of path, e.g.
    serialize-item -id "{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}"
  • recurse - switch which decides if serialization concerns only the single item or the whole tree below the item, e.g.
    serialize-item -path "master:\content\articles" -recurse
  • target - directory where the serialized files should be saved, e.g.
    serialize-item -path "master:\content" -target "c:\tmp"
  • itempathsabsolute - works only with target param and decides if folder structure starting from "sitecore\content" should be created, e.g. if you want to serialize articles item in directory c:\tmp\sitecore\content you can use
    serialize-item -path "master:\content\articles" -itempathsabsolute -target "c:\tmp"

deserialize-item

Deserialization of items with Sitecore Powershell Extensions uses deserialize-item command. The simplest syntax requires 2 parameters:
  • path - which is a path to the item on the drive but without .item extension. If the item does not exist in the Sitecore tree yet, you need to pass the parent item path.
  • root - the directory which is the root of serialization. Trailing slash \ character is required
, e.g.:
deserialize-item -path "c:\project\data\serialization\master\sitecore\content\articles" -root "c:\project\data\serialization\"
There are some other parameters you can use to extend deserialization:
  • recurse - decides if the whole tree beneath the item should be deserialized, e.g.
    deserialize-item -path "c:\project\data\serialization\master\sitecore\content\articles" -root "c:\project\data\serialization\" -recurse
  • usenewid - tells Sitecore if each of the items should be created with a newly generated ID, e.g.
    deserialize-item -path "c:\project\data\serialization\master\sitecore\content\articles" -root "c:\project\data\serialization\" -usenewid -recurse
  • disableevents - if set Sitecore will use EventDisabler during deserialization, e.g.
    deserialize-item -path "c:\project\data\serialization\master\sitecore\content\articles" -root "c:\project\data\serialization\" -disableevents -recurse
Serialization and deserialization are really easy with Sitecore Powershell Extensions. Other Sitecore Powershell Extensions resources can be found in this blog post. If you have any questions or comments, leave a comment below or ask Adam Najmanowicz, the author of the Extensions. And if you're interested in more information about Sitecore, check out my other
Sitecore blog posts.