Sitecore PowerShell Extensions - Custom function
Last time I had to prepare a resolution for making changes
on the existing content after the deployment. We were adding new components to
Standard Values of the page and they had to be configured properly on the
content item by setting data source. Change like this couldn’t be synchronised
by Unicorn as the page content item was one-time deployment only item (NewItemOnlyEvaluator)
and setting data source straight on Standard Value is a really bad idea!
To create a custom SPE function we need to have new module created. The easiest it will be by using wizard:
We will need Shared Functions integration, so we can select it just while creating the module.
It can be done after as well just by creating scripts library called Functions. Here's link to documentation: https://doc.sitecorepowershell.com/modules/integration-points/functions
After this we will have our module folder with Functions folder inside. There we will create our custom shared function.
I have named it Set-RenderingDatasource. Body of the function should be placed inside field described as Script body - Source code written in PowerShell. That's the function I've shown earlier.
Next thing will be create new PowerShell Script Library item, just on the same level as Functions item. I named it as After Deploy Scripts and created one new script inside - Apply-Datasources:
This script will use our custom shared function to apply datasources to items. Here is the code:
First crucial thing is to import our shared function
Then I have added a flag for rolling back changes if needed. That's a subject of decision how we should do the rollback. For this example rollback will be simply setting empty datasources to that items - as they are empty at beginning.
Under I've added variables that will contain ID of the page where we want make our updates, references to rendering items we want to set datasource and ids of datasources. All those things should be known, as we are assuming they will be deployed by, e.g. unicorn (new renderings and datasources - one time deployment) or they are in the content already for some time - the page we want to update.
Then, based on the rollback flag, I'm using our imported function to set up datasources
Let's roll now! Let's edit our script in ISE!
And run it to see the result:
It's good to log the things script does and say something at the end of execution.
Done! I hope that was interesting lesson of using Sitecore PowerShell to apply changes after deployments. Enjoy!
We had three options there:
- Set data source manually after synch
- Create a Sitecore content package with prepared changes
- Prepare Sitecore PowerShell script that will automate the changes we need
As doing things manually is always error-prone and package
could overwrite some content changes, we decided to go for third option –
PowerShell scripting!
First of all, I needed a way for setting data source on rendering
that already exists on a page content item. I found there is
not such specific function in SPE core so I wrote a custom one.
To create a custom SPE function we need to have new module created. The easiest it will be by using wizard:
We will need Shared Functions integration, so we can select it just while creating the module.
It can be done after as well just by creating scripts library called Functions. Here's link to documentation: https://doc.sitecorepowershell.com/modules/integration-points/functions
After this we will have our module folder with Functions folder inside. There we will create our custom shared function.
I have named it Set-RenderingDatasource. Body of the function should be placed inside field described as Script body - Source code written in PowerShell. That's the function I've shown earlier.
Next thing will be create new PowerShell Script Library item, just on the same level as Functions item. I named it as After Deploy Scripts and created one new script inside - Apply-Datasources:
First crucial thing is to import our shared function
Import-Function -Name Set-RenderingDatasource
Then I have added a flag for rolling back changes if needed. That's a subject of decision how we should do the rollback. For this example rollback will be simply setting empty datasources to that items - as they are empty at beginning.
Under I've added variables that will contain ID of the page where we want make our updates, references to rendering items we want to set datasource and ids of datasources. All those things should be known, as we are assuming they will be deployed by, e.g. unicorn (new renderings and datasources - one time deployment) or they are in the content already for some time - the page we want to update.
Then, based on the rollback flag, I'm using our imported function to set up datasources
Set-RenderingDatasource -Page $updatedPage -Rendering $modalRendering -DatasourceId $modalDatasourceId -Device Default -Publish
Let's roll now! Let's edit our script in ISE!
And run it to see the result:
It's good to log the things script does and say something at the end of execution.
Done! I hope that was interesting lesson of using Sitecore PowerShell to apply changes after deployments. Enjoy!