Created
October 14, 2016 15:21
-
-
Save AdamNaj/354660183bea96a4ceb06cf1a9a92f30 to your computer and use it in GitHub Desktop.
Working with Links database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assuming Sitecore PowerShell Extensions 4.2 or newer is installed | |
# Assuming your Home has an "Image" field of type "Image" | |
# Assuming you have second item next to Home called Home2 that has an "Image" field of type "Image" | |
$coverImage = Get-Item 'master:\media library\Default Website\cover' | |
$scLogoImage = Get-item 'master:\media library\Default Website\sc_logo' | |
function Get-HomeItem { Get-item master:\content\home } | |
function Get-HomeChildItem { Get-item master:\content\Home\child } | |
function Get-Home2Item { Get-item master:\content\home2 } | |
function Set-CoverImage { | |
Write-Host "`nReset 'home', 'child' and 'home2' to link to 'cover'- 3 items" -Fore Magenta | |
(Get-HomeItem).Image = $coverImage | |
(Get-HomeChildItem).Image = $coverImage | |
(Get-Home2Item).Image = $coverImage | |
Get-ItemReferrer -Item $coverImage | |
} | |
#Let's try some dumb global operations first | |
Set-CoverImage | |
Write-Host "`nRelinking all instances of 'cover' image to 'sc_logo'" -Fore Yellow | |
$coverImage | Update-ItemReferrer -NewTarget $scLogoImage | |
Write-Host "`n'cover' should no longer have links leading to it - 0 items " -Fore red | |
$coverImage | Get-ItemReferrer | |
Write-Host "`n'sc_logo' should now be linked from all - 3 items" -Fore green | |
$scLogoImage | Get-ItemReferrer | |
Write-Host "`nRemoving links to 'sc_logo' from all items" -Fore Yellow | |
$scLogoImage | Update-ItemReferrer -RemoveLink | |
Write-Host "`n'sc_logo' should have no links to it - 0 items" -Fore red | |
$scLogoImage | Get-ItemReferrer | |
#Let's try some filtering... | |
Set-CoverImage | |
Write-Host "`nRemove links to the 'cover' image from all items under 'master:\content\home'" -Fore Yellow | |
Get-ChildItem master:\content\home -WithParent -Recurse | # get items under home | |
Get-ItemReference -ItemLink | # get all items that they are refering to | |
? { $_.TargetItemID -eq $coverImage.ID } | # filter only references to $coverImage | |
Update-ItemReferrer -RemoveLink # remove links | |
Write-Host "`n'cover' should have 1 link leading from 'home2'" -Fore green | |
$coverImage | Get-ItemReferrer | |
Set-CoverImage | |
Write-Host "`nUpdate all links to 'cover' image to point to 'sc_logo' from all immediate children of /sitecore/content" -Fore Yellow | |
Get-ChildItem master:\content | # get items immediately under 'under home'content' | |
Get-ItemReference -ItemLink | # get all items that they are refering to | |
? { $_.TargetItemID -eq $coverImage.ID } | # filter only references to $coverImage | |
Update-ItemReferrer -NewTarget $scLogoImage | |
Write-Host "`n'cover' should have link from home2/child - 1 item" -Fore green | |
$coverImage | Get-ItemReferrer | |
Write-Host "`n'sc_logo' should have links leading from 'home' and 'home2' - 2 items" -Fore green | |
$scLogoImage | Get-ItemReferrer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment