Friday, August 30, 2019

How to force ItemUpdating/ItemUpdated events in event receivers for all list items in Sharepoint list via PowerShell

If you need to force ItemUpdating/ItemUpdated events in all event receivers attached to Sharepoint list you may go through each item one by one, click Edit item and then click Save. However if there are many list items it is better to use PowerShell script which will force these events for all list items one by one:

param(
    [string]$url,
    [string]$listTitle
)

$web = Get-SPWeb $url
$list = $web.Lists[$listTitle]
foreach ($item in $list.Items)
{
    $item.Update()
    Start-Sleep -Seconds 5
}

In this script we iterate trough all list items in the list and call SPListItem.Update() method. In turn it forces attached event receivers to generate ItemUpdating and ItemUpdated events. After each update we wait 5 seconds to make sure event receivers finish previous event before to handle next one. If this is not needed for your scenario or if event receivers work faster you may comment or decrease number of seconds to wait.

1 comment: