Last week during my Sitecore PowerShell Extensions presentation at the Sitecore User Group Conference 2014 — a conference held in Utrecht, Netherlands — I demonstrated how to invoke PowerShell scripts from the Item context menu in Sitecore, and felt I should capture what I had shown in a blog post — yes, this is indeed that blog post. 😉
During that piece of my presentation, I shared the following PowerShell script to expands tokens in fields of a Sitecore item (if you want to learn more about tokens in Sitecore, please take a look at John West’s post about them, and also be aware that one can also invoke the Expand-Token PowerShell command that comes with Sitecore PowerShell Extensions to expand tokens on Sitecore items — this makes things a whole lot easier 😉 ):
$item = Get-Item . $tokenReplacer = [Sitecore.Configuration.Factory]::GetMasterVariablesReplacer() $item.Editing.BeginEdit() $tokenReplacer.ReplaceItem($item) $item.Editing.EndEdit() Close-Window
The script above calls Sitecore.Configuration.Factory.GetMasterVariablesReplacer() for an instance of the MasterVariablesReplacer class — which is defined and can be overridden in the “MasterVariablesReplacer” setting in your Sitecore instance’s Web.config — and passes the context item — this is denote by a period — to the MasterVariablesReplacer instance’s ReplaceItem() method after the item has been put into editing mode.
Once the Item has been processed, it is taken out of editing mode.
So how do we save this script so that we can use it in the Item context menu? The following screenshot walks you through the steps to do just that:
The script is saved to an Item created by the dialog above:
Let’s test this out!
I selected an Item with unexpanded tokens:
I then launched its Item context menu, and clicked the option we created to ‘Expand Tokens’:
As you can see the tokens were expanded:
If you have any questions or thoughts on this, please drop a comment.
Until next time, have a scriptolicious day 😉
MEOW