Update Conflict Error in SharePoint Using PowerShell Script

Introduction

Many of you must have come across the issue “An update conflict has occurred and you must re-try this action” and it is suggested to remove the file system cache on the front-end servers. There are many articles to show how to do this manually. But here I will show you how to resolve it using a PowerShell script.

Cause for this issue

This issue occurs if the contents of the file system cache on the front-end servers are newer than the contents of the configuration database.

Resolution

To resolve this issue, clear the file system cache on all servers in the server Farm on which the Windows SharePoint Services Timer service is running. This can be done manually, but consider a large Farm where the administrator needs to login to each of the servers to perform the activity manually. It is time-consuming and people can make mistakes. PowerShell becomes handy here.

The following procedure can resolve the issue:

  1. Stop the SharePoint timer service
  2. Take a backup of the system cache files before deleting it
  3. Delete the XML files from the cache folder
  4. Reset the cache.ini file to the value 1
  5. Start the SharePoint timer service

Now I will explain how to perform the preceding tasks using a PowerShell script.

Stop SharePoint timer service

The following piece of code stops the SharePoint timer service:

  1. Function StopSPTimerServicesInFarm([Microsoft.SharePoint.Administration.SPFarm]$farm)  
  2. {  
  3.     Write-Host ""  
  4.     foreach($server in $farm.Servers)  
  5.         {  
  6.         foreach($instance in $server.ServiceInstances)  
  7.                 {  
  8.                         # If the server has the timer service then stop the service  
  9.                        if($instance.TypeName -eq $timerServiceInstanceName)  
  10.                        {  
  11.                             [string]$serverName = $server.Name  
  12.    
  13.                             Write-Host "Stop " $timerServiceName " service on server: " $serverName -fore yellow  
  14.                                
  15.                             $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"  
  16.                             $serviceInternalName = $service.Name  
  17.                             sc.exe \\$serverName stop $serviceInternalName > $null  
  18.   
  19.                               # Wait until this service has actually stopped  
  20.                   write-host "Waiting for '$timerServiceName' service on server: " $serverName " to be stopped" -fore yellow  
  21.                               do  
  22.                       {  
  23.                               Start-Sleep 1  
  24.                               Write-Host -foregroundcolor DarkGray -NoNewLine "."  
  25.                               $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"  
  26.                       }  
  27.                      while ($service.State -ne "Stopped")  
  28.                  write-host " '$timerServiceName' service on server: " $serverName " stopped successfully" -fore green              
  29.                              break;  
  30.                        }  
  31.                 }  
  32.         }  
  33.    
  34.         Write-Host ""  
  35. }  
Backup config cache files

The following piece of code backs up the config cache files before deleting it:
  1. Function BackupSPConfigFiles([Microsoft.SharePoint.Administration.SPFarm]$farm)  
  2. {  
  3.     write-host ""  
  4.     write-host "Backup SP config cache files" -fore yellow  
  5.       
  6.     foreach($server in $farm.servers)  
  7.     {  
  8.         foreach($instance in $server.ServiceInstances)  
  9.         {  
  10.             if($instance.TypeName -eq $timerServiceInstanceName)  
  11.             {  
  12.                 $ServerName = $server.name  
  13.                 $FolderName  = $servername + "SPConfigCacheBackup"  
  14.                 write-host "Creating a folder to hold the backup files" -fore magenta  
  15.                 write-host "Checking whether the folder aleady exist"  
  16.                 if(Test-path $scriptbase\$FolderName)  
  17.                 {  
  18.                     write-host "Folder already exists and the script is deleting it......"  
  19.                     remove-item $scriptbase\$FolderName -recurse -confirm:$false   
  20.                     write-host "Existing folder deleted" -fore green  
  21.                 }  
  22.                 else  
  23.                 {  
  24.                     write-host "Folder does not exist"  
  25.                 }  
  26.                 New-Item $scriptbase\$FolderName -type directory  
  27.                 write-host "New folder created to hold the backup files" -fore magenta  
  28.                 write-host "Backup of SP config files for the server " $serverName " started ...... " -fore yellow  
  29.                 $path = "\\" + $serverName + "\c$\ProgramData\Microsoft\SharePoint\Config"  
  30.                 Copy-Item $path -Destination $scriptbase\$FolderName -recurse  
  31.                 write-host "SP config cache files backed up for the server " $serverName " Sucessfully..." -fore green  
  32.             }  
  33.         }  
  34.     }  
  35.     write-host "SP config caches files are backed up" -fore green  
  36.     write-host ""  
  37. }  
Deleting XML files

The following piece of code deletes the XML files:
  1. Function DeletingXMLFiles([Microsoft.SharePoint.Administration.SPFarm]$farm)  
  2. {  
  3.     Write-host ""  
  4.     write-host "Deleting SP config cache XML files from each server in the farm" -fore yellow  
  5.     $path = ""  
  6.     foreach($server in $farm.servers)  
  7.     {  
  8.         foreach($instance in $server.ServiceInstances)  
  9.         {  
  10.             if($instance.TypeName -eq $timerServiceInstanceName)  
  11.             {  
  12.                 $serverName = $server.Name  
  13.                 write-host "Deleting SP config cache files from the server " $servername -fore magenta  
  14.                 $path = "\\" + $serverName + "\c$\ProgramData\Microsoft\SharePoint\Config\*-*\*.xml"  
  15.                 remove-item -path $path -force  
  16.                 write-host "SP config cache files deleted on the server " $servername -fore magenta  
  17.                 break  
  18.             }  
  19.         }  
  20.     }  
  21.     write-host "SP config cache XML files from each server in the farm is deleted successfully......" -fore green  
  22.     write-host ""  
  23. }  
Reset timer cache

The following piece of code resets the timer cache:
  1. Function ResetTimerCache([Microsoft.SharePoint.Administration.SPFarm]$farm)  
  2. {  
  3.     write-host ""  
  4.     write-host "Reseting the value of timer cache to 1" -fore yellow  
  5.     $path = ""  
  6.     foreach($server in $farm.servers)  
  7.     {  
  8.         foreach($instance in $server.ServiceInstances)  
  9.         {  
  10.             if($instance.TypeName -eq $timerServiceInstanceName)  
  11.             {  
  12.                 $serverName = $server.Name  
  13.                 write-host "Reseting the value of timer cache file in server " $serverName -fore magenta  
  14.                 $path = "\\" + $serverName + "\c$\ProgramData\Microsoft\SharePoint\Config\*-*\cache.ini"  
  15.                 Set-Content -path $path -Value "1"  
  16.                 write-host "Value of timer cache file in server " $serverName " is resetted to 1" -fore magenta  
  17.                 break  
  18.             }  
  19.         }  
  20.     }  
  21.     write-host "The value of timer cache is resetted to 1 in all the SP servers in the farm" -fore green  
  22.     write-host ""  
  23. }  
Start SharePoint timer service
  1. Function StartSPTimerServicesInFarm([Microsoft.SharePoint.Administration.SPFarm]$farm)  
  2. {  
  3.     Write-Host ""  
  4.     foreach($server in $farm.Servers)  
  5.         {  
  6.         foreach($instance in $server.ServiceInstances)  
  7.                 {  
  8.                         # If the server has the timer service then stop the service  
  9.                        if($instance.TypeName -eq $timerServiceInstanceName)  
  10.                        {  
  11.                             [string]$serverName = $server.Name  
  12.    
  13.                             Write-Host "Start " $timerServiceName " service on server: " $serverName -fore yellow  
  14.                                
  15.                             $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"  
  16.                             $serviceInternalName = $service.Name  
  17.                             sc.exe \\$serverName start $serviceInternalName > $null  
  18.   
  19.                               # Wait until this service starts running  
  20.                   write-host "Waiting for " $timerServiceName " service on server: " $serverName " to be started" -fore yellow  
  21.                               do  
  22.                       {  
  23.                               Start-Sleep 1  
  24.                               Write-Host -foregroundcolor DarkGray -NoNewLine "."  
  25.                               $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"  
  26.                       }  
  27.                      while ($service.State -ne "Running")  
  28.                  write-host $timerServiceName " service on server: " $serverName " started successfully" -fore green              
  29.                              break;  
  30.                        }  
  31.                 }  
  32.         }  
  33.    
  34.         Write-Host ""  
  35. }  
Complete Code
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\ClearSPConfigCachePatch-$LogTime.rtf"  
  3.  
  4. # Add SharePoint PowerShell Snapin  
  5.   
  6.   
  7. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {  
  8.     Add-PSSnapin Microsoft.SharePoint.Powershell  
  9. }  
  10.   
  11. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  12. Set-Location $scriptBase  
  13.  
  14.  
  15. #Deleting any .rtf files in the scriptbase location  
  16. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  17. if($FindRTFFile)  
  18. {  
  19.     foreach($file in $FindRTFFile)  
  20.         {  
  21.             remove-item $file  
  22.         }  
  23. }  
  24.   
  25.   
  26. start-transcript $logfile  
  27.   
  28. Write-host "##############Following steps will be involved################" -fore green  
  29. write-host "Step 1 - Stop the timerservice" -fore cyan  
  30. write-host "Step 2 - Take the backup of config cache file" -fore cyan  
  31. write-host "Step 3 - Delete the XML files" -fore cyan  
  32. write-host "Step 4 - Reset the cache.ini file" -fore cyan  
  33. write-host "Step 5 - Start the SP timerservice" -fore cyan  
  34. Write-host "##############Above steps will be involved##################" -fore green  
  35.   
  36. $global:timerServiceName = "SharePoint 2010 Timer"  
  37. $global:timerServiceInstanceName = "Microsoft SharePoint Foundation Timer"  
  38.  
  39. # Get the local farm instance  
  40. [Microsoft.SharePoint.Administration.SPFarm]$farm = [Microsoft.SharePoint.Administration.SPFarm]::get_Local()  
  41. Function StopSPTimerServicesInFarm([Microsoft.SharePoint.Administration.SPFarm]$farm)  
  42. {  
  43.     Write-Host ""  
  44.     foreach($server in $farm.Servers)  
  45.         {  
  46.         foreach($instance in $server.ServiceInstances)  
  47.                 {  
  48.                         # If the server has the timer service then stop the service  
  49.                        if($instance.TypeName -eq $timerServiceInstanceName)  
  50.                        {  
  51.                             [string]$serverName = $server.Name  
  52.    
  53.                             Write-Host "Stop " $timerServiceName " service on server: " $serverName -fore yellow  
  54.                                
  55.                             $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"  
  56.                             $serviceInternalName = $service.Name  
  57.                             sc.exe \\$serverName stop $serviceInternalName > $null  
  58.   
  59.                               # Wait until this service has actually stopped  
  60.                   write-host "Waiting for '$timerServiceName' service on server: " $serverName " to be stopped" -fore yellow  
  61.                               do  
  62.                       {  
  63.                               Start-Sleep 1  
  64.                               Write-Host -foregroundcolor DarkGray -NoNewLine "."  
  65.                               $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"  
  66.                       }  
  67.                      while ($service.State -ne "Stopped")  
  68.                  write-host " '$timerServiceName' service on server: " $serverName " stopped successfully" -fore green              
  69.                              break;  
  70.                        }  
  71.                 }  
  72.         }  
  73.    
  74.         Write-Host ""  
  75. }  
  76.   
  77.   
  78. Function BackupSPConfigFiles([Microsoft.SharePoint.Administration.SPFarm]$farm)  
  79. {  
  80.     write-host ""  
  81.     write-host "Backup SP config cache files" -fore yellow  
  82.       
  83.     foreach($server in $farm.servers)  
  84.     {  
  85.         foreach($instance in $server.ServiceInstances)  
  86.         {  
  87.             if($instance.TypeName -eq $timerServiceInstanceName)  
  88.             {  
  89.                 $ServerName = $server.name  
  90.                 $FolderName  = $servername + "SPConfigCacheBackup"  
  91.                 write-host "Creating a folder to hold the backup files" -fore magenta  
  92.                 write-host "Checking whether the folder aleady exist"  
  93.                 if(Test-path $scriptbase\$FolderName)  
  94.                 {  
  95.                     write-host "Folder already exists and the script is deleting it......"  
  96.                     remove-item $scriptbase\$FolderName -recurse -confirm:$false   
  97.                     write-host "Existing folder deleted" -fore green  
  98.                 }  
  99.                 else  
  100.                 {  
  101.                     write-host "Folder does not exist"  
  102.                 }  
  103.                 New-Item $scriptbase\$FolderName -type directory  
  104.                 write-host "New folder created to hold the backup files" -fore magenta  
  105.                 write-host "Backup of SP config files for the server " $serverName " started ...... " -fore yellow  
  106.                 $path = "\\" + $serverName + "\c$\ProgramData\Microsoft\SharePoint\Config"  
  107.                 Copy-Item $path -Destination $scriptbase\$FolderName -recurse  
  108.                 write-host "SP config cache files backed up for the server " $serverName " Sucessfully..." -fore green  
  109.             }  
  110.         }  
  111.     }  
  112.     write-host "SP config caches files are backed up" -fore green  
  113.     write-host ""  
  114. }  
  115.   
  116.   
  117. Function DeletingXMLFiles([Microsoft.SharePoint.Administration.SPFarm]$farm)  
  118. {  
  119.     Write-host ""  
  120.     write-host "Deleting SP config cache XML files from each server in the farm" -fore yellow  
  121.     $path = ""  
  122.     foreach($server in $farm.servers)  
  123.     {  
  124.         foreach($instance in $server.ServiceInstances)  
  125.         {  
  126.             if($instance.TypeName -eq $timerServiceInstanceName)  
  127.             {  
  128.                 $serverName = $server.Name  
  129.                 write-host "Deleting SP config cache files from the server " $servername -fore magenta  
  130.                 $path = "\\" + $serverName + "\c$\ProgramData\Microsoft\SharePoint\Config\*-*\*.xml"  
  131.                 remove-item -path $path -force  
  132.                 write-host "SP config cache files deleted on the server " $servername -fore magenta  
  133.                 break  
  134.             }  
  135.         }  
  136.     }  
  137.     write-host "SP config cache XML files from each server in the farm is deleted successfully......" -fore green  
  138.     write-host ""  
  139. }  
  140.   
  141.   
  142. Function ResetTimerCache([Microsoft.SharePoint.Administration.SPFarm]$farm)  
  143. {  
  144.     write-host ""  
  145.     write-host "Reseting the value of timer cache to 1" -fore yellow  
  146.     $path = ""  
  147.     foreach($server in $farm.servers)  
  148.     {  
  149.         foreach($instance in $server.ServiceInstances)  
  150.         {  
  151.             if($instance.TypeName -eq $timerServiceInstanceName)  
  152.             {  
  153.                 $serverName = $server.Name  
  154.                 write-host "Reseting the value of timer cache file in server " $serverName -fore magenta  
  155.                 $path = "\\" + $serverName + "\c$\ProgramData\Microsoft\SharePoint\Config\*-*\cache.ini"  
  156.                 Set-Content -path $path -Value "1"  
  157.                 write-host "Value of timer cache file in server " $serverName " is resetted to 1" -fore magenta  
  158.                 break  
  159.             }  
  160.         }  
  161.     }  
  162.     write-host "The value of timer cache is resetted to 1 in all the SP servers in the farm" -fore green  
  163.     write-host ""  
  164. }  
  165.   
  166.   
  167. Function StartSPTimerServicesInFarm([Microsoft.SharePoint.Administration.SPFarm]$farm)  
  168. {  
  169.     Write-Host ""  
  170.     foreach($server in $farm.Servers)  
  171.         {  
  172.         foreach($instance in $server.ServiceInstances)  
  173.                 {  
  174.                         # If the server has the timer service then stop the service  
  175.                        if($instance.TypeName -eq $timerServiceInstanceName)  
  176.                        {  
  177.                             [string]$serverName = $server.Name  
  178.    
  179.                             Write-Host "Start " $timerServiceName " service on server: " $serverName -fore yellow  
  180.                                
  181.                             $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"  
  182.                             $serviceInternalName = $service.Name  
  183.                             sc.exe \\$serverName start $serviceInternalName > $null  
  184.   
  185.                               # Wait until this service starts running  
  186.                   write-host "Waiting for " $timerServiceName " service on server: " $serverName " to be started" -fore yellow  
  187.                               do  
  188.                       {  
  189.                               Start-Sleep 1  
  190.                               Write-Host -foregroundcolor DarkGray -NoNewLine "."  
  191.                               $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"  
  192.                       }  
  193.                      while ($service.State -ne "Running")  
  194.                  write-host $timerServiceName " service on server: " $serverName " started successfully" -fore green              
  195.                              break;  
  196.                        }  
  197.                 }  
  198.         }  
  199.    
  200.         Write-Host ""  
  201. }  
  202.      
  203.  
  204. #############Calling functions################  
  205. StopSPTimerServicesInFarm $farm  
  206. BackupSPConfigFiles $farm  
  207. DeletingXMLFiles $farm  
  208. ResetTimerCache $farm  
  209. StartSPTimerServicesInFarm $farm  
  210. ##########################################  
  211.   
  212. write-host "SCRIPT COMPLETED" -fore cyan  
  213. stop-transcript  
Execution Procedure

Step 1: Download the script and copy it to the SharePoint server.

Step 2: Navigate to the script path.

Step 3: Execute the script.



Conclusion

Thus this article explained how to troubleshoot the “Update conflict” error in SharePoint using a PowerShell script.