Below Power Shell attaches the event handler to a document library.
I have initialized a sequence number and increment this number every time we attach an event handler.
In this article I want to share how to use Power Shell to attach event receiver to a list, provide configuration values from an xml.
And also, how to use Power Shell to view a list of existing event receivers.
If that’s your case you could try to use a workaround of delaying the event handler for a couple of seconds (using Thread.
Sleep() ) which of course is far from an elegant solution or you could see whether you can move your code to Item Updated() or Item Updating() rather than Item Added().
This is because, we need to specify a different sequence number for each event handler to indicate the order in which they should be executed. Web Url; Write-Host($web Url); ## open the web site Write-Host "Opening web $web Url" -nonewline $web = Get-SPWeb $web Url Write-Host "$web" Write-host " – done " -foreground green foreach($handler in $site.
# Setup the module for Share Point if ((Get-PSSnapin -Name Microsoft. Power Shell -Error Action Silently Continue) -eq $null ) #Read the data from the Xml file $config = (Get-Content "Config.xml") if ($config -eq $null ) $site = $config. Event Handler) Id is used for the Type of event handler. Invalid Receiver = -1, Item Adding = 1, Item Updating = 2, Item Deleting = 3, Item Checking In = 4, Item Checking Out = 5, Item Unchecking Out = 6, Item Attachment Adding = 7, Item Attachment Deleting = 8, Item File Moving = 9, Field Adding = 101, Field Updating = 102, Field Deleting = 103, List Adding = 104, List Deleting = 105, Site Deleting = 201, Web Deleting = 202, Web Moving = 203, Web Adding = 204, Workflow Starting = 501, Item Added = 10001, Item Updated = 10002, Item Deleted = 10003, Item Checked In = 10004, Item Checked Out = 10005, Item Unchecked Out = 10006, Item Attachment Added = 10007, Item Attachment Deleted = 10008, Item File Moved = 10009, Item File Converted = 10010, Field Added = 10101, Field Updated = 10102, Field Deleted = 10103, List Added = 10104, List Deleted = 10105, Site Deleted = 10201, Web Deleted = 10202, Web Moved = 10203, Web Provisioned = 10204, Workflow Started = 10501, Workflow Postponed = 10502, Workflow Completed = 10503, Email Received = 20000, Context Event = 32766 We used the below configuration file to provide the configuration values to the powershell script.All of them call the internal method Update Internal() which has some interesting arguments like The experiment showed that all of them but one were leading to an exception being thrown. So one way to fix the problem is changing your event receiver to update the list item using System Update(false).Some people are additionally recommending to disable the event firing when calling the update as well: This will work well unless you actually want to create a new version of the list item or call any of the other update methods for whateever reason.This error occurs when you upload a file to a document library through the Share Point UI and when the document library has an event receiver which updates the list item on Item Added(). on ….” and the callstack leads to a COMException in the Microsoft. It is running in a single thread but it represents exactly what is happening with the event receiver and the Edit pages if you think of it this way: All objects with index “1” are accessed from the event receiver and all objects with index “2”are accessed from the Edit code-behind.The error doesn’t happen all the time which lead us to conclude that it is due to some race conditions, especially knowing that event receivers are run in separate thread. The issue only happens when the code is executed in the exact sequence listed above.All this explains why the issue is not happening all the time but only when specific race conditions are met.