4

I'm building some scripts that will check that items are localized to a particular branch of the content tree (we have lots of parallel tree structures and we want to copy between them) and can successfully retrieve all links/references an item contains using Get-ItemReference with the -ItemLink parameter.

Is there a command I can use on the links returned to update them to refer to different items?

The closest I can find is Update-ItemReferrer though this seems to be the reverse (updates items that point to a specific item) and also possibly deprecated (I get a no command found error, and the page in the gitbook has format issues https://doc.sitecorepowershell.com/appendix/common/update-itemreferrer )

So are the link/reference objects returned able to be updated directly, or do I need to use them to find the fields on the item itself and update there?

1 Answer 1

4

Does this example point you in the right direction?

Example: The following re-links images.

$item = Get-Item -Path "master:\media library\images\koala"
$itemNew = Get-Item -Path "master:\media library\images\penguins"
$links = Get-ItemReferrer -Item $item -ItemLink
foreach($link in $links) {
    $linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID 
    $itemField = $linkedItem.Fields[$link.SourceFieldID]
    $field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField)

    $linkedItem.Editing.BeginEdit()
    $field.Relink($link, $itemNew)
    $linkedItem.Editing.EndEdit() | Out-Null
}

Check out the gist here: https://gist.github.com/michaellwest/f563b0b3597f6c0a75d6

Example: The following gets the list of references to the item.

$item = Get-Item -Path "master:\content\home\sample item 1"
$links = Get-ItemReference -Item $item -ItemLink
foreach($link in $links) {
    # Filter out the links you care about and relink if necessary
}
1
  • Yes, that works - though I'll be using Get-ItemReferences, as I'm going in the other direction, so I'll have begin/end edit outside the foreach. I was hoping the link objects themselves were editable, but using them to grab the correct fields will also work. I'm assuming the $field.Relink method works on Rich text fields with multiple links? Sep 18, 2017 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.