Tags

, , ,

Hi Friends,

I have faced this issue a few times in the past but have never got a chance to explore and find the root cause of the issues.

I started with an issue and end up fixing a completely different location/issue.

My challenge started with, When executing the item referrer, it never returned all items. First I thought it is due to the fact that the items are in Bucket and the item referrer is not reading the buckets. But that is not correct.

When we call the item referrer, it gets the referrers from the links table in the Core database.

Link database is very important to find out which Item is created from the template or which item is referred to by another item. If this DB is not updated correctly, There will be issues in updating the referred items after publishing and then indexing.

In some cases, the link database (Technically it is a table but for the sake of explanation in terms of Sitecore, I am writing Link Database) is not updated correctly. One scenario is, that you restored the master from the production to the development or UAT environment. In this case, the Items are published but the Links DB is not updated.

For my case, I tried to run the Rebuild link database from Control Panel. But it was failing or had a memory exception. So I was not able to rebuild the whole DB.

Rebuild Link DB option in Control Panel

Same issue was reported on this post at Sitecore Stack Exchange https://sitecore.stackexchange.com/questions/30267/link-database-rebuild-specific-items-instead-of-whole-database/32515#32515

Can I rebuild the links DB for a single item? The answer is Yes.

Find out if the item has links. Like, take a template ID and run the below query in SQL. This will give details of the links. If that is not as you expect, you are missing the items.

select * from links where SourceDatabase = 'master'
and TargetItemID = '{TemplateID}'

This is a similar scenario. If an item is changed, it should be Re-indexed in the indexes. Same way, the item should be added/updated in the Links DB as well.

Option 1: If it’s a single item, save the item. It will update the DB.

Option 2: If you have many items – Run the PowerShell script. It calls the UpdateReferences function of Sitecore. In the below script, update the Path at line no 6.

Line 16 adds the item in the Links Database.

The advantage of the below script is, that you don’t need to save/update anything in the master database to update the Link DB. And this is much faster to update compared to rebuilding the whole link database.

<#
Script created by : Maulik Darji
Reference: https://darjimaulik.wordpress.com
#>

$StartItemPath = "/content/shared content/Content/People/"

function Update-ItemReferences
{
    $count = 1
    $items = Get-ChildItem -Path $StartItemPath -Recurse
    $totalCount = $items.count
    write-host "No of items to update is $totalCount"
    foreach ($item in $items)
    {
        [Sitecore.Globals]::LinkDatabase.UpdateReferences($item)
        if (0 -eq $count % 100){
            write-host "$count of $totalCount is complete."
        }
        $count++
        #$item
    }
  
    
    write-host "$count items are updated"
}


$Items = Update-ItemReferences 
Close-Window

When you are running this, make sure you run for a smaller part of the content tree. Running this on the whole content tree will be time-consuming.

Feel free to comment if you have any queries.

Enjoy!