{"id":1270,"date":"2012-01-17T00:00:00","date_gmt":"2012-01-17T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/managing-itemupdating-and-itemupdated-events-firing-twice-in-a-sharepoint-item-event-receiver\/"},"modified":"2021-05-11T15:56:24","modified_gmt":"2021-05-11T15:56:24","slug":"managing-itemupdating-and-itemupdated-events-firing-twice-in-a-sharepoint-item-event-receiver","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/managing-itemupdating-and-itemupdated-events-firing-twice-in-a-sharepoint-item-event-receiver\/","title":{"rendered":"Managing ItemUpdating and ItemUpdated Events Firing Twice in a SharePoint Item Event Receiver"},"content":{"rendered":"<div id=\"pretty\">\n<p class=\"start\">I&#8217;m usually disappointed when writers employ oft-overused metaphors to describe a situation. With that in mind, SharePoint 2010 is like a sea of icebergs &#8211; there is a lot going on under the surface that you may not notice until it&#8217;s too late. Unfortunately, that makes your project like the Titanic. I don&#8217;t mean that it&#8217;s largest and most luxurious application every written, but rather that you may be cruising headlong into a nasty rendezvous with an iceberg that could deal a severe blow to your project. We may never know about all of the dangers lurking out there, but today we&#8217;re going to cover at least one danger you may encounter while writing event receivers &#8211; an annoying issue with the <strong>ItemUpdating<\/strong> and <strong>ItemUpdated<\/strong> events firing twice. I should also point out that I know the difference between a metaphor and simile in case that was bothering you from the opening sentence. I am nothing if not a masterful linguist after a beer or two or more. <\/p>\n<h2>What is an Item Event Receiver? <\/h2>\n<p>Hopefully you know about item event receiver if you are having problems with them firing twice. If not, kudos to you for tackling the object model with reckless abandon. Sometimes that is the most exciting way to learn, but for those less adventurous I will briefly cover the topic here. You can think of an item event receiver like a database trigger: it has different events that fire during the course of SharePoint running an operation on a list item (or document item). <\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><strong>Event Name<\/strong><\/p>\n<\/td>\n<td valign=\"top\">\n<p><strong>Synchronous<\/strong><\/p>\n<\/td>\n<td valign=\"top\">\n<p><strong>Description<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" colspan=\"3\">\n<p><strong>Item Events<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemAdding<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Yes<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Runs before an item is added to the list.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemAdded<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<td valign=\"top\">\n<p>Runs after an item has been added to the list.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemDeleting<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Yes<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Runs before an item is deleted from the list.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemDeleted<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<td valign=\"top\">\n<p>Runs after an item has been deleted from the list.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemUpdating<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Yes<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Runs before an item is updated in the list.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemUpdated<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<td valign=\"top\">\n<p>Runs after an item has been updated in the list.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" colspan=\"3\">\n<p><strong>Check-In Events<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemCheckingIn<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Yes<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Runs before an item is checked in.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemCheckedIn<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<td valign=\"top\">\n<p>Runs after an item has been checked in.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemCheckingOut<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Yes<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Runs before an item is checked out.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemCheckedOut<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<td valign=\"top\">\n<p>Runs after an item has been checked out.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemUncheckingOut<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Yes<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Runs before an item is un-checked out.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemUncheckedOut<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<td valign=\"top\">\n<p>Runs after an item has been un-checked out.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" colspan=\"3\">\n<p><strong>File Events<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemFileConverted<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<td valign=\"top\">\n<p>Runs before a file in a doc library is converted to a different file type.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemFileMoving<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Yes<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Runs before a file is moved.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemFileMoved<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<td valign=\"top\">\n<p>Runs after a file has been moved.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" colspan=\"3\">\n<p><strong>Attachment Events<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemAttachmentAdding<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Yes<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Runs before an attachment is added to an item.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemAttachmentAdded<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<td valign=\"top\">\n<p>Runs after an attachment has been added to an item.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemAttachmentDeleting<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Yes<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Runs before an attachment is deleted from an item.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ItemAttachmentDeleting<\/p>\n<\/td>\n<td valign=\"top\"> <\/td>\n<td valign=\"top\">\n<p>Runs after an attachment has been deleted from an item.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Item Event Receivers derive from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventreceiver.aspx\">SPItemEventReceiver<\/a> class and have a number of methods that can be overridden to respond to various events: <\/p>\n<p>As you look through this list, you should notice that events have two types of endings: <\/p>\n<ol>\n<li>Events ending in <strong>ING<\/strong>:\n<ul>\n<li>Examples include <strong>ItemAdding<\/strong>, <strong>ItemDeleting<\/strong>, <strong>ItemUpdating<\/strong>, etc.  <\/li>\n<li>These events occur <strong>BEFORE<\/strong> the action takes place. This means that you have a chance to modify the item or cancel the operation before it occurs.  <\/li>\n<li>These event handlers run synchronously &#8211; they occur in order and one must complete before the next is run.  <\/li>\n<li>You may be able to modify property values in the event handler.\n<ul>\n<li>Each event method has a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventproperties.aspx\">SPItemEventProperties<\/a> parameter named properties. If you wish to modify a property value on the list item during the event, the value should be updated in <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventproperties.afterproperties.aspx\">AfterProperties<\/a> property of the properties parameter. SharePoint reads these values from the event parameter and modifies the item accordingly when the actual operation runs (e.g. the actual Add \/ Update operation for which the Adding \/ Updating event is being fired). Do NOT try to manually get the list item in code and update a property on it because the optimistic locking mechanism in SharePoint may throw an error later on when the operation associated with the event to which you are responding attempts to complete.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Events ending in <strong>ED<\/strong>:\n<ul>\n<li>Examples include <strong>ItemAdded<\/strong>, <strong>ItemDeleted<\/strong>, <strong>ItemUpdated<\/strong>, etc.  <\/li>\n<li>These events occur <strong>AFTER<\/strong> the action takes place. This means that you can respond to the event but you cannot cancel it or modify anything about it.  <\/li>\n<li>These events handlers may run synchronously or asynchronously depending on how they are configured. Asynchronous operations may occur in any order and complete in order.  <\/li>\n<li>Property modification is <strong>not available<\/strong> in an asynchronous event handler (even if it is running synchronously).\n<ol>\n<li>Although asynchronous events expose a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventproperties.aspx\">SPItemEventProperties<\/a> parameter named properties just like their synchronous counterparts, remember that the operation has already completed so you cannot modify anything in the properties parameter (well, you can, but it doesn&#8217;t do anything). Additionally, the properties parameter may not be populated with information that you would tend to expect to be present. <\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<div class=\"note\">\n<p class=\"note\"><strong>WARNING<\/strong>: One major gotcha you should know about the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventreceiver.aspx\">SPItemEventReceiver<\/a> class is that while you can implement multiple list item event handlers in a single class, SharePoint instantiates a new instance of that class for each individual event it needs to handle. What this means is that you cannot store data in instance-level variables and share that data between event handlers. For example, if you define an instance level variable in the class to store data in the <strong>ItemUpdating<\/strong> event, then try to access that data in the <strong>ItemUpdated<\/strong> event, you will find that the data is not there when you go to check it in the <strong>ItemUpdated<\/strong> event. This is because you have two classes &#8211; one that is handling the <strong>ItemUpdating<\/strong> event and in which the instance level variable is set, and one that is handling the <strong>ItemUpdated<\/strong> event in which the instance level variable is not set. <\/p>\n<\/div>\n<h2>When Do the ItemUpdating and ItemUpdated Events Fire Twice? <\/h2>\n<p>Simply put, the <strong>ItemUpdating<\/strong> and <strong>ItemUpdated<\/strong> fire twice when adding a document to a library that has the <strong>Require Check <\/strong><strong>Out<\/strong> option enabled. To understand why this is happening, let&#8217;s first look at what happens when the user adds a document to the library when the <strong>Require Check Out<\/strong> option is disabled: <\/p>\n<ul>\n<li>User clicks on the Add a Document link  <\/li>\n<li>SharePoint displays the Upload a Document dialog  <\/li>\n<li>User uploads the Document to SharePoint  <\/li>\n<li>SharePoint fires the <strong>ItemAdding<\/strong> Event  <\/li>\n<li>SharePoint adds the document to the Library  <\/li>\n<li>SharePoint fires the <strong>ItemAdded<\/strong> event  <\/li>\n<li>SharePoint displays the file in the library <\/li>\n<\/ul>\n<p>So the net result of this is that the document is uploaded and the <strong>ItemAdding<\/strong> and <strong>ItemAdded<\/strong> events have fired, which is pretty much what you would expect. Next, let&#8217;s look at what happens when the user adds a document when the <strong>Require Check Out<\/strong> option is enabled. <\/p>\n<ul>\n<li>User clicks on the Add a Document link  <\/li>\n<li>SharePoint displays the Upload a Document dialog  <\/li>\n<li>User uploads the Document to SharePoint  <\/li>\n<li>SharePoint fires the <strong>ItemAdding<\/strong> Event  <\/li>\n<li>SharePoint adds the document to the Library  <\/li>\n<li>SharePoint fires the <strong>ItemAdded<\/strong> event  <\/li>\n<li>SharePoint displays the property editing screen to the user  <\/li>\n<li>User edits any properties they wish to change and clicks the Save button  <\/li>\n<li>SharePoint fires the <strong>ItemUpdating<\/strong> event (first time it is fired)  <\/li>\n<li>SharePoint sets any document property field entered in the property editor dialog  <\/li>\n<li>SharePoint fires the <strong>ItemUpdated<\/strong> event (first time it is fired)  <\/li>\n<li>SharePoint now begins the process of automatically checking in the document  <\/li>\n<li>SharePoint fires the <strong>ItemUpdating<\/strong> event (second time it is fired)  <\/li>\n<li>SharePoint fires the <strong>ItemCheckingIn<\/strong> event  <\/li>\n<li>SharePoint fires the <strong>ItemUpdated<\/strong> event (second time it is fired)  <\/li>\n<li>SharePoint fires the <strong>ItemCheckedIn<\/strong> event <\/li>\n<\/ul>\n<p>The first time the <strong>ItemUpdating<\/strong> and <strong>ItemUpdated<\/strong> events fire it is in response to the document properties changing. The second time they fire it is in response to the document being checked in. It appears as though they are firing twice in this situation because SharePoint is updating the properties on the document and then checking it in on the same request. If you were to check the document out and edit the properties on the document, you would see the <strong>ItemUpdating<\/strong> and <strong>ItemUpdated<\/strong> events fire once. Later on, when you checked the document in, you would see those events fire again. So the double-event firing isn&#8217;t a bug, it&#8217;s just a result of the automatic check-in that occurs when you first add a document to a document library. <\/p>\n<div class=\"note\">\n<p class=\"note\"><strong>Note<\/strong><strong>:<\/strong><strong> <\/strong>when the property editor dialog displays, the user has the option to cancel out of the dialog. If the user opts to cancel, then only the <strong>ItemAdding<\/strong> and <strong>ItemAdded<\/strong> events will have fired and the document will be left in a checked out state. <\/p>\n<p><strong>Also note<\/strong>: the <strong>ItemUpdating<\/strong> and <strong>ItemUpdated<\/strong> events that fire in response to the properties being edited from the dialog will always occur, even if the user is not entering or changing any of the values. <\/p>\n<\/div>\n<h2>Turning Off the Require Check Out Option <\/h2>\n<p>One option for fixing the double-event firing issue is to simply turn it off. You can locate the setting using the following instructions: <\/p>\n<ul>\n<li>Click on a list in the left navigation menu (or otherwise get into a list)  <\/li>\n<li>Click the <strong>Library<\/strong> tab in the Ribbon  <\/li>\n<li>Click <strong>Library Settings<\/strong> button in the Library Tab  <\/li>\n<li>This will take you to the list information screen  <\/li>\n<li>Click the <strong>Versioning settings<\/strong> link  <\/li>\n<li>The <strong>Require Check Out<\/strong> radio button should appear in the list of settings <\/li>\n<\/ul>\n<h2>Programmatically Determining the Cause of ItemUpdating and ItemUpdated Firing <\/h2>\n<p>Turning off the Require Check Out option is a great quick fix if you don&#8217;t require the item to be checked out in order for it to be edited. But that option exists to be used, and some people really do need it. If you find yourself in this situation, then you&#8217;ll have to solve the problem in code. Fortunately, there is a relatively simple way to check whether the <strong>ItemUpdating<\/strong> and <strong>ItemUpdated<\/strong> events are firing in response to a check-in outlined in <a href=\"http:\/\/support.microsoft.com\/kb\/939307\">Knowledgebase Article 939307<\/a>. You just have to check to see if the <strong>vti_sourcecontrolcheckedoutby<\/strong> property on the item was cleared: <\/p>\n<pre class=\"lang:c# theme:vs2012\">public override void ItemUpdating(SPItemEventProperties properties)\n{\n&#160;&#160;&#160;&#160;if (properties.AfterProperties[\"vti_sourcecontrolcheckedoutby\"] == null \n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &amp;amp;&amp;amp; properties.BeforeProperties[\"vti_sourcecontrolcheckedoutby\"] != null)\n&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\/\/Code to run if event trigged by a check-in\n&#160;&#160;&#160;&#160;}\n&#160;&#160;&#160;&#160;else\n&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\/\/Code to run if event trigged by something else\n&#160;&#160;&#160;&#160;}\n}<\/pre>\n<p>This code is using the <strong>BeforeProperties<\/strong> and <strong>AfterProperties<\/strong> on the <strong>properties<\/strong> parameter to see what the value of the <strong>vti_sourcecontrolcheckedoutby<\/strong> property on the item was before the update occurred, and what it will be after the update has completed. The <strong>vti_sourcecontrolcheckedoutby<\/strong> property identifies who the item is currently checked-out to. As such, if the item is being checked-in, the <strong>BeforeProperties<\/strong> will contain a value for the property and the <strong>AfterProperties<\/strong> will not. It&#8217;s a pretty simple fix, but we can definitely make it a bit more reusable for everyone on a development team and reduce the hassle of having to remember the specifics about how to run the check in their <strong>ItemUpdating<\/strong> and <strong>ItemUpdated<\/strong> event handlers. <\/p>\n<h2>Creating a Base Class for Event Receivers <\/h2>\n<p>Following is the code for a base class that adds a new parameter to the <strong>ItemUpdating<\/strong> and <strong>ItemUpdated<\/strong> methods that specifies whether the event was called as a result of a check-in operation. <\/p>\n<pre class=\"lang:c# theme:vs2012\">\/\/\/ &lt;summary&gt;\n\/\/\/&#160;&#160; Base class for ItemEventReceivers that helps developers determine if the ItemUpdating \n\/\/\/&#160;&#160; and ItemUpdated events were thrown as a result of a Check-In operation.&#160;&#160;Please see \n\/\/\/&#160;&#160; http:\/\/support.microsoft.com\/kb\/939307 for more information.\n\/\/\/ &lt;\/summary&gt;&#160;&#160;&#160;&#160;\npublic abstract class SPItemEventReceiverBase : SPItemEventReceiver\n{\n&#160;&#160;&#160;&#160;\/\/\/ &lt;summary&gt;\n&#160;&#160;&#160;&#160;\/\/\/&#160;&#160; Contains the SharePoint field name checked when determining if the item was checked-in.\n&#160;&#160;&#160;&#160;\/\/\/ &lt;\/summary&gt;\n&#160;&#160;&#160;&#160;private const string SourceControlFieldName = \"vti_sourcecontrolcheckedoutby\";\n\n\n&#160;&#160;&#160;&#160;\/\/\/ &lt;summary&gt;\n&#160;&#160;&#160;&#160;\/\/\/&#160;&#160; Runs when the ItemUpdated event is raised.\n&#160;&#160;&#160;&#160;\/\/\/ &lt;\/summary&gt;\n&#160;&#160;&#160;&#160;\/\/\/ &lt;param id=\"properties\"\"&gt;SharePoint event properties.&lt;\/param&gt;\n&#160;&#160;&#160;&#160;\/\/\/ &lt;param id=\"isCheckIn\"\"&gt;Contains a boolean value indicating whether the method is being run in response to a check-in operation.&lt;\/param&gt;\n&#160;&#160;&#160;&#160;public virtual void ItemUpdated(SPItemEventProperties properties, bool isCheckIn)\n&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;}\n\n\n&#160;&#160;&#160;&#160;\/\/\/ &lt;summary&gt;\n&#160;&#160;&#160;&#160;\/\/\/&#160;&#160; Runs when the ItemUpdating event is raised.\n&#160;&#160;&#160;&#160;\/\/\/ &lt;\/summary&gt;\n&#160;&#160;&#160;&#160;\/\/\/ &lt;param id=\"properties\"\"&gt;SharePoint event properties.&lt;\/param&gt;\n&#160;&#160;&#160;&#160;\/\/\/ &lt;param id=\"isCheckIn\"\"&gt;Contains a boolean value indicating whether the method is being run in response to a check-in operation.&lt;\/param&gt;\n&#160;&#160;&#160;&#160;public virtual void ItemUpdating(SPItemEventProperties properties, bool isCheckIn)\n&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;}\n\n\n&#160;&#160;&#160;&#160;\/\/\/ &lt;summary&gt;\n&#160;&#160;&#160;&#160;\/\/\/&#160;&#160; Determines whether the ItemUpdated event has been raised as a result of a check-in \n&#160;&#160;&#160;&#160;\/\/\/&#160;&#160; operation and then passes that information to the overloaded ItemUpdated method.\n&#160;&#160;&#160;&#160;\/\/\/ &lt;\/summary&gt;\n&#160;&#160;&#160;&#160;\/\/\/ &lt;param id=\"properties\"\"&gt;SharePoint event properties.&lt;\/param&gt;\n&#160;&#160;&#160;&#160;public sealed override void ItemUpdated(SPItemEventProperties properties)\n&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if (properties.AfterProperties[SourceControlFieldName] == null &amp;amp;&amp;amp; properties.BeforeProperties[SourceControlFieldName] != null)\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ItemUpdated(properties, true);\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;else\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ItemUpdated(properties, false);\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;base.ItemUpdated(properties);\n&#160;&#160;&#160;&#160;}\n\n\n&#160;&#160;&#160;&#160;\/\/\/ &lt;summary&gt;\n&#160;&#160;&#160;&#160;\/\/\/&#160;&#160; Determines whether the ItemUpdated event has been raised as a result of a check-in \n&#160;&#160;&#160;&#160;\/\/\/&#160;&#160; operation and then passes that information to the overloaded ItemUpdated method.\n&#160;&#160;&#160;&#160;\/\/\/ &lt;\/summary&gt;\n&#160;&#160;&#160;&#160;\/\/\/ &lt;param id=\"properties\"\"&gt;SharePoint event properties.&lt;\/param&gt;\n&#160;&#160;&#160;&#160;public sealed override void ItemUpdating(SPItemEventProperties properties)\n&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if (properties.AfterProperties[SourceControlFieldName] == null &amp;amp;&amp;amp; properties.BeforeProperties[SourceControlFieldName] != null)\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ItemUpdating(properties, true);\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;else\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ItemUpdating(properties, false);\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;base.ItemUpdating(properties);\n&#160;&#160;&#160;&#160;}\n\n} \/\/class <\/pre>\n<p>So, let&#8217;s talk about the code for a second. You should notice that we&#8217;ve added two new methods: <\/p>\n<ul>\n<li>public virtual void <strong>ItemUpdating<\/strong>(SPItemEventProperties properties, bool isCheckIn)  <\/li>\n<li>public virtual void <strong>ItemUpdated<\/strong>(SPItemEventProperties properties, bool isCheckIn) <\/li>\n<\/ul>\n<p>These methods are just like the <strong>ItemUpdating<\/strong> and <strong>ItemUpdated<\/strong> methods in the <strong>SPItemEventReceiver<\/strong> class, but they have an additional Boolean parameter named <strong>isCheckIn<\/strong> that indicates whether or not the event is being raised as result of a check-in operation. They are declared as virtual methods and have empty method bodies because we want developers to override them in a derived class. <\/p>\n<p>Next, we have overridden the standard <strong>ItemUpdating<\/strong> and <strong>ItemUpdated<\/strong> methods defined in the <strong>SPItemEventReceiver<\/strong> class. In both methods we make the check to see if the <strong>vti_sourcecontrolcheckedoutby<\/strong> item property has been cleared out and then call our new <strong>ItemUpdating<\/strong> or <strong>ItemUpdated<\/strong> method with the appropriate Boolean value to indicate whether the event was called as a result of a check-in operation. Notice that both of these method are marked with the sealed keyword. This ensures that developers in a derived class do not accidentally override this method instead of the new one. <\/p>\n<h2>Using the Base Class <\/h2>\n<p>To use the base class in your project, just have your Item Event Receiver classes inherit from <strong>SPItemEventReceiverBase<\/strong> instead of <strong>SPItemEventReciver<\/strong>. Then override the new <strong>ItemUpdating<\/strong> or <strong>ItemUpdated<\/strong> methods and use the <strong>isCheckIn<\/strong> parameter to ensure you don&#8217;t run your event receiver code more than you need to run it. <\/p>\n<pre class=\"lang:c# theme:vs2012\">public override void ItemUpdating(SPItemEventProperties properties, bool isCheckIn)\n{\n&#160;&#160;&#160;&#160;if (isCheckin)\n&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\/\/Code to run if event trigged by a check-in\n&#160;&#160;&#160;&#160;}\n&#160;&#160;&#160;&#160;else\n&#160;&#160;&#160;&#160;{\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;\/\/Code to run if event trigged by something else\n&#160;&#160;&#160;&#160;}\n} <\/pre>\n<div class=\"note\">\n<p class=\"note\"><strong>Warning<\/strong>: Do not call <strong>base.ItemUpdating<\/strong><strong>(<\/strong><strong>properties)<\/strong> or <strong>base.ItemUpdated<\/strong><strong>(properties)<\/strong> from inside an overridden <strong>IteamUpdating<\/strong> or <strong>ItemUpdating<\/strong> method when using the <strong>SPItemEventReciverBase<\/strong>. This will cause an infinite recursion loop and instead of solving the &#8220;It&#8217;s firing twice problem&#8221; you will now have an &#8220;It&#8217;s firing 1000 times problem.&#8221;<\/p>\n<\/div>\n<hr \/>\n<div class=\"note\">\n<p class=\"note\"> \t<a href=\"http:\/\/www.red-gate.com\/labs\/ants-performance-profiler\/?utm_source=simpletalk&amp;utm_medium=article&amp;utm_campaign=antsperformanceprofiler&amp;utm_content=sharepoint-managingItemUpdating\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1555-ANTS_60x60_Logo_CP.gif\" class=\"float-left\" alt=\"1555-ANTS_60x60_Logo_CP.gif\" \/><\/a> \tHow does your SharePoint application perform? The Beta release of ANTS Performance Profiler 8 adds support for SharePoint 2013, helping rapidly identify and understand SharePoint performance issues. <a href=\"http:\/\/www.red-gate.com\/labs\/ants-performance-profiler\/?utm_source=simpletalk&amp;utm_medium=article&amp;utm_campaign=antsperformanceprofiler&amp;utm_content=sharepoint-managingItemUpdating\">Try the Beta<\/a>.<\/p>\n<\/p><\/div>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Developing a Sharepoint application would have all the fun of a video game, if only you had infinite lives. Dangers lurk hidden out there which, if you run into them, can be a blow to your project and waste a great deal of time. Damon gives just one example of a poisoned dagger in the game of Sharepoint Development: The Item Event Receiver.&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538],"tags":[4143,4204,4770],"coauthors":[],"class_list":["post-1270","post","type-post","status-publish","format-standard","hentry","category-dotnet-development","tag-net","tag-net-tools","tag-sharepoint"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=1270"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1270\/revisions"}],"predecessor-version":[{"id":90918,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1270\/revisions\/90918"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1270"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}