I have blogged before about the usefulness of the Sitecore Powershell Extensions module and how it has saved my keister on more than one occasion. More recently, I have started using it to replace traditional Sitecore modules, such as the ones found on marketplace.sitecore.net. My reasoning for this is threefold:
- Sitecore Powershell Extensions already contains hooks for many of the integration points that traditional modules typically interact with. This includes things like Event Handlers, Scheduled Tasks, Commands, Context Menus, Reports, and more.
- Traditional modules are almost always tied to a specific Sitecore.Kernel version, making them difficult to update and maintain, assuming that the source code is even available at all.
- With the exception of TDS's Package Validation feature, there are limited ways to ensure that a module is installed across your local, development, QA, and production environments..
A good example of a module that SPE can replace is an XML sitemap generator. As of this posting, there are a total of 11 sitemap modules available in the marketplace, each with varying degrees of support for the features I need. It can be quite a daunting task to evaluate them all!
As an SPE script, we can avoid the pain points of traditional modules, yet still have a sitemap solution that is both powerful and flexible.
To get started, we'll want to register a new function in SPE. This will make the function available to import from any other script. This step is not strictly needed, but it will help keep things organized and allow us to call upon it whenever we need. To register a function, create a new PowerShell Script item under the following node: /sitecore/system/Modules/PowerShell/Script Library/Platform/Functions. Then, paste the script below into the Script body field. NOTE: you may need to click "Elevate session" on the item in order to see this field.
Now that the function is registered, we can call upon it like so:
Import-Function -Name Create-XmlSitemap Create-XmlSitemap
The function takes several parameters that determine how the sitemap should be generated. To learn more about what each of them does, you can run the following command:
Import-Function -Name Create-XmlSitemap Get-Help Create-XmlSitemap -Full
There are a number of ways in which you can trigger the script to run. To run it as a Schedule Task, for example, you can simply right-click the /sitecore/system/Tasks/Schedules item and insert a new PowerShell Scripted Task Schedule item. You can also choose to run it when a publishing event occurs, remotely, or as a post build step.
UPDATE: October 4, 2017
- Added recursive function to crawl site - fixes timeouts with large trees
- Added support for white-listing and black-listing multiple fields
- Added support for writing sitemap to media item
- Added support for publishing media item