Sitecore

Sitecore Powershell 6: Working with Item References

Istock 843015650

We have seen scenarios where we want to know about Item references. For instance, suppose we want to get a list of items that have SideContent Rendering assigned to their presentation. This is the exact kind of information that we are going to extract using different commands detailed in this post.

One thing to note is that these commands will make use of Link Database, so your Link Database should be up-to-date.

Note: If you have not updated your Link Database, I recommend to update it before performing below operations using Sitecore Powershell.

Get-ItemReferrer

This command is used to get all the items that refer to the item specified in the command. So, suppose we want to get list of items that refer to our SideContent rendering. Following is the script that can accomplish that using Get-ItemReferrer:

 

Get-ItemReferrer -ID "{13AAD693-D140-4054-840D-4F9DABF681D3}" | Format-Table -Property Name,Id

Here, we are using the ID of our rendering to be used in Get-ItemReferrer command.

Output of the above script:

Spereference1

You can see the list has all kinds of items that refer to our rendering item.

We can use Where-Object to filter out our result set based on TemplateName. We only want to know the names of items created from Event Details Template that refer to our item.

Get-ItemReferrer -ID "{13AAD693-D140-4054-840D-4F9DABF681D3}"  | Where-Object { $_.TemplateName -eq "Event Details" }

 

Output of above command:

Spereference2

 

Get-ItemReference

This command will return a list of items that the specified item refers to. In other words, this command will retrieve a list of items that are referred by the specified item.

Let’s check what items are linked to Home Page item:

Get-ItemReference -ID "{D45691EA-32A0-44D3-B56C-7B70A0CFA65E}" | Format-Table -Property Name,Id, TemplateName

 

Output of the above:

Spereference3

 

Using -ItemLink with Get-ItemReference

Using -ItemLink will force the Get-ItemReference command to return a list of Links rather than Items where you can find more information about the association between your target item and source item.

 

Get-ItemReference -ID "{D45691EA-32A0-44D3-B56C-7B70A0CFA65E}" -ItemLink

 

Output after running the above command:

Sitecore - Understanding Development Approaches: A Sitecore Outlook
Understanding Development Approaches: A Sitecore Outlook

Designing, building, and implementing top-notch experiences not only requires a great deal of planning, strategy, and time – it also requires the right digital experience platform (DXP) and the right development approach for your business needs.

Get the Guide

Spereference4

 

As you can see, the information retrieved after adding –ItemLink has information about link between target and source. Here you can find the Source Field ID and Target Item ID that is assigned to this field. Database, versioning information among others.

Let’s format out output a bit using Select-Object to output only Source Field ID and Target Item ID:

Get-ItemReference -ID "{D45691EA-32A0-44D3-B56C-7B70A0CFA65E}" -ItemLink | Select-Object -Property SourceFieldID,TargetItemID

 

Output of the above:

Spereference5

 

Update-ItemReferrer

Let’s suppose we have a scenario where we want to update all the references of the DecorationBanner image field from EventBanner image media item to NewEventBanner image media item.

$bannerImage = Get-Item -Path "master:" -ID "{ECDF3D59-73C1-41A3-9F87-ACD56900765F}"

$newImage = Get-Item -Path "master:" -ID "{1CBD0EE7-0CD5-4A8B-8626-EDB0F0599A5F}"

$bannerImage | Update-ItemReferrer -NewTarget $newImage

 

In the above script, we are taking the current banner image item and the new banner image item in separate variables. Then, we are performing Update-ItemReferrer command with $bannerImage as input and -NewTarget specified with $newImage.

But, we have one problem here. This is a global operation as there is no filtering performed here. What if you only want them to be updated in items created from Event Details template? This will require us to write some custom script that will provide filtered input to Update-ItemReferrer command. We will only include children and grandchildren of Home Page items which are created from Event Details template. Then, we will use –ItemLink and Where-Object (?) to get link information of only fields which refer to $bannerImage media item. And then we will be passing it to Update-ItemReferrer command as input.

Following will be the script that we will be using to filter as per our requirement:

$bannerImage = Get-Item -Path "master:" -ID "{ECDF3D59-73C1-41A3-9F87-ACD56900765F}"

$newImage = Get-Item -Path "master:" -ID "{1CBD0EE7-0CD5-4A8B-8626-EDB0F0599A5F}"

$EventDetailItems = Get-ChildItem -Path "master:" -ID "{D45691EA-32A0-44D3-B56C-7B70A0CFA65E}" -Recurse | Where-Object { $_.TemplateName -eq "Event Details" }

$EventDetailItems | Get-ItemReference -ItemLink | Where-Object { $_.TargetItemID -eq $bannerImage.ID }


 

Output of the above:

Spereference6

 

As you can see, we only have 4 rows in our result because we only have 4 items created from Event Details template under Home Page. Also, you can see that the table shows information about link because we used –ItemLink. You will also notice that the TargetItemID is the same. This is because we filtered the result based on ID of our EventBanner media item.

Now, as we are done with our filtering, let’s add Update-ItemReferrer command to our script and pass our filtered list as input.

The script would be then:

#Get reference to current a target image item

$bannerImage = Get-Item -Path "master:" -ID "{ECDF3D59-73C1-41A3-9F87-ACD56900765F}"
$newImage = Get-Item -Path "master:" -ID "{1CBD0EE7-0CD5-4A8B-8626-EDB0F0599A5F}"


#Filter our result based on parent node and TemplateName
$EventDetailItems = Get-ChildItem -Path "master:" -ID "{D45691EA-32A0-44D3-B56C-7B70A0CFA65E}" -Recurse | Where-Object { $_.TemplateName -eq "Event Details" }

$LinkList = $EventDetailItems | Get-ItemReference -ItemLink | ? { $_.TargetItemID -eq $bannerImage.ID }




#Execute Update-Referrer command on filtered result set
$LinkList | Update-ItemReferrer  -NewTarget $newImage

 

I executed the above script. There would be no output as its updating the links so to check if our field has been updated, I ran this script:

Get-ChildItem -Path "master:" -ID "{D45691EA-32A0-44D3-B56C-7B70A0CFA65E}" -Recurse | Where-Object { $_.TemplateName -eq "Event Details" } | ForEach{
    $_._.DecorationBanner.MediaID

}

 

Output of above:

Spereference7

 

As you can see from the above image, our DecorationBanner field has been updated with $newImage item.

We saw how we can use Get-ItemReferrer, Get-ItemReference and Update-ItemReferrer in this post. These are very useful commands when we want to extract and manipulate information of item references.

The next post will be about how we can use Sitecore Powershell commands to create reports and export them.

 

 

 

 

 

 

 

About the Author

Ramakant Chandel is working as a Sitecore Professional. He likes to explore challenging and new technical concepts in Sitecore. He is passionate about sharing his knowledge with everyone.

More from this Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Categories