The utility functions for Sitecore PowerShell.
- Download the package from here and install it.
- Declare
Import-Function SpeExtendedFunctions
in the top of your script. - Now you can use the additional functions.
- Disable-Security
- Edit-Item
- Get-MediaContent
- New-ItemIfNotExists
- Resolve-Instance
- Set-FinalWorkflow
- Set-InitialWorkflow
- Set-MediaContent
- Switch-Context
- Switch-Database
- Switch-Language
- Switch-Site
- Switch-User
Apply the SecurityDisabler
in a specified script block.
Disable-Security {
# Able to call unauthorized process here.
}
Allow item editing in a specified script block.
$item = Get-Item "/sitecore/content/Home"
Edit-Item $item {
$item["Title"] = "New Title"
}
echo $item["Title"] # => Returns "New Title".
Get the Media
field value of a media item as string.
$media = Get-Item "/sitecore/media library/My CSV file"
Get-MediaContent -Item $media # => Returns the csv content attached to $media.
If an item does not exist in a specified path, it will be created with a specified template. If the item exists, it will be returned.
$template = Get-Item "/sitecore/templates/Sample/Sample Item"
New-ItemIfNotExists -Path "/sitecore/content/Home/New Item" -TemplateID $template.ID
# => The item will always be returned.
Resolve an instance by interface registered in the DI container.
Resolve-Instance -TypeName Sitecore.XA.Foundation.Search.Services.IIndexResolver
# => Returns an instance of the IndexResolver.
Assign a final workflow to a specified item.
$item = Get-Item "/sitecore/content/Home"
Set-FinalWorkflow -Item $item # => Assigned the Approved state.
Assign a initial workflow to a specified item.
$item = Get-Item "/sitecore/content/Home"
Set-InitialWorkflow -Item $item # => Assigned the Draft state.
Set a string value to the Media
field of a media item.
$media = Get-Item "/sitecore/media library/My CSV file"
Set-MediaContent -Item $media -Content "New Content"
Get-MediaContent -Item $media # => Returns "New Content"
Switch Site/Database/Language/User context in a specified block. When -NoSecurity
is passed, apply the SecurityDisabler
.
$context = @{
Database = "web"
Language = "ja-JP"
Site = "website"
User = "sitecore\Author"
NoSecurity = $true
}
Switch-Context @context -Script {
[Sitecore.Context]::Database.Name # => Returns "web"
[Sitecore.Context]::Language.Name # => Returns "ja-JP"
[Sitecore.Context]::Site.Name # => Returns "website"
[Sitecore.Context]::User.Name # => Returns "sitecore\Author"
}
Apply the DatabaseSwitcher
in a specified script block.
[Sitecore.Context]::Database.Name # => Returns "master"
Switch-Database "web" {
[Sitecore.Context]::Database.Name # => Returns "web"
}
Apply the LanguageSwitcher
in a specified script block.
[Sitecore.Context]::Language.Name # => Returns "en"
Switch-Language "ja-JP" {
[Sitecore.Context]::Language.Name # => Returns "ja-JP"
}
Apply the SiteContextSwitcher
in a specified script block.
[Sitecore.Context]::Site.Name # => Returns "shell"
Switch-Site "website" {
[Sitecore.Context]::Site.Name # => Returns "website"
}
Apply the UserSwitcher
in a specified script block.
[Sitecore.Context]::User.Name # => Returns "sitecore\Admin"
Switch-User "sitecore\Author" {
[Sitecore.Context]::User.Name # => Returns "sitecore\Author"
}
- Takumi Yamada ([email protected])
Cairngorm is licensed under the MIT license. See LICENSE.txt.