Why the event handler is not trigged from console app ?  A few days ago when try to test an event handler, I've build a simple console app. and add from code an item to a list. Surprise ! ItemAdded() was not called. Here is the Main() method:<br><br>static void Main(string[] args)<br>       {<br>           using (SPSite site = new SPSite(&quot;http://ovidiubeches/&quot;))<br>           {<br>               using (SPWeb web = site.OpenWeb(&quot;dt&quot;))<br>               {<br>                   web.AllowUnsafeUpdates = true;<br><br>                   SPList list = web.Lists[&quot;Tasks&quot;];<br>                   SPListItem item = list.Items.Add();<br>                   item[&quot;Title&quot;] = &quot;new item &quot; + DateTime.Now.ToString();<br>                   item.Update();<br><br>                   web.AllowUnsafeUpdates = false;<br>               }<br>           }<br>       }<br><br>So the even hanlder  is working perfect when add item from browser or from a webpart or from a webservice. What is the answer for this ? Maybe you ask why do you want add an item from console app?! Why not ? What if I want to have a unit testing ?<br><br>many thanx,<br><br>Ovidiu© 2009 Microsoft Corporation. All rights reserved.Thu, 16 Jul 2009 13:28:57 Z5405c69e-edda-4988-9976-3827256762f6http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#5405c69e-edda-4988-9976-3827256762f6http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#5405c69e-edda-4988-9976-3827256762f6ovulexhttp://social.msdn.microsoft.com/Profile/en-US/?user=ovulexWhy the event handler is not trigged from console app ?  A few days ago when try to test an event handler, I've build a simple console app. and add from code an item to a list. Surprise ! ItemAdded() was not called. Here is the Main() method:<br><br>static void Main(string[] args)<br>       {<br>           using (SPSite site = new SPSite(&quot;http://ovidiubeches/&quot;))<br>           {<br>               using (SPWeb web = site.OpenWeb(&quot;dt&quot;))<br>               {<br>                   web.AllowUnsafeUpdates = true;<br><br>                   SPList list = web.Lists[&quot;Tasks&quot;];<br>                   SPListItem item = list.Items.Add();<br>                   item[&quot;Title&quot;] = &quot;new item &quot; + DateTime.Now.ToString();<br>                   item.Update();<br><br>                   web.AllowUnsafeUpdates = false;<br>               }<br>           }<br>       }<br><br>So the even hanlder  is working perfect when add item from browser or from a webpart or from a webservice. What is the answer for this ? Maybe you ask why do you want add an item from console app?! Why not ? What if I want to have a unit testing ?<br><br>many thanx,<br><br>OvidiuSun, 31 Aug 2008 18:26:14 Z2008-08-31T18:26:14Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#3768cc2c-22fe-40df-b426-5aa0a1b72597http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#3768cc2c-22fe-40df-b426-5aa0a1b72597Gary Lapointehttp://social.msdn.microsoft.com/Profile/en-US/?user=Gary%20LapointeWhy the event handler is not trigged from console app ? I've done lots of stuff with adding items via the command line (via custom stsadm extensions) and I've never had an issue with event handlers not firing.  Maybe it has to do with AllowUnsafeUpdates being set to true - you don't need to set this for command line operations.Sun, 31 Aug 2008 22:55:36 Z2008-08-31T22:55:36Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#ee371ed7-0dce-4aee-a5f8-f0dde50224a7http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#ee371ed7-0dce-4aee-a5f8-f0dde50224a7Moonis Tahirhttp://social.msdn.microsoft.com/Profile/en-US/?user=Moonis%20TahirWhy the event handler is not trigged from console app ? did you check event log to see if there was any issue. I think event handler was triggered but some code may have issue in your event handler. if you can check event log and sharepoint Logs directory log files also that should give you the idea of possible fix. <br>If possible if you can paste some ItemAdded event code that will be great. Mon, 01 Sep 2008 00:52:31 Z2008-09-01T00:52:31Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#8cd4dc95-64a1-4a7a-ad84-df604869e4adhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#8cd4dc95-64a1-4a7a-ad84-df604869e4adErucyhttp://social.msdn.microsoft.com/Profile/en-US/?user=ErucyWhy the event handler is not trigged from console app ? It seems like the Event Handler of SharePoint only trigged in a Context environment ( in the w3wp.exe process, that means the WebPart, the WebApplication, the Web Services, or the Event Handler it self).<br>It cannot trigged in a console application, because it's not running under the &quot;web&quot;. I think it's by design......<br>Mon, 01 Sep 2008 03:27:18 Z2008-09-01T03:27:18Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#6d9de3b7-0ade-40b6-b09b-55fee69f364ahttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#6d9de3b7-0ade-40b6-b09b-55fee69f364aovulexhttp://social.msdn.microsoft.com/Profile/en-US/?user=ovulexWhy the event handler is not trigged from console app ? Yes Erucy, I think same right now .... but do have somebody have a explanation for this ? <br>Here is the event handler code : ...as I said before it is working perfect in other situations :)<br><br> <p> public class ListSyncEventHandler : SPItemEventReceiver<br>    {        public override void ItemAdded(SPItemEventProperties properties)<br>        {<br>            DisableEventFiring();</p> <p>            try<br>            {<br>                Logging.LogMessage(&quot;info&quot;, &quot;start ListSyncEventHandler.ItemAdded&quot;);            }<br>            catch (Exception ex)<br>            {<br>                Logging.LogMessage(&quot;Exception&quot;, &quot;Exception in ItemAdded&quot;);<br>                Logging.LogException(ex);<br>                throw ex;<br>            }</p> <p>            EnableEventFiring();<br>        }<br></p> .....<br>}<br><br>So I do invite other sharepoint programmer to try add items from a custom app console (c#/vb)... not stsadm<br><br>many thanx,<br><br>OvidiuMon, 01 Sep 2008 20:14:46 Z2008-09-01T20:14:46Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#6759e6d2-3596-4a3c-a4aa-01ed369e699ehttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#6759e6d2-3596-4a3c-a4aa-01ed369e699eGary Lapointehttp://social.msdn.microsoft.com/Profile/en-US/?user=Gary%20LapointeWhy the event handler is not trigged from console app ? STSADM is just a console app - there's nothing special about it - there is no web context.  Did you check your event log to see if there's any errors?Tue, 02 Sep 2008 01:09:45 Z2008-09-02T01:09:45Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#43db5216-ada1-4f53-9b2e-8ef8999f53e1http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#43db5216-ada1-4f53-9b2e-8ef8999f53e1JakeJBlueshttp://social.msdn.microsoft.com/Profile/en-US/?user=JakeJBluesWhy the event handler is not trigged from console app ? Hello Togehter,<br><br>it seems very funny, we had the same problem.<br><br>The ItemAdded-Event seems not to be fired, when the application, which requests a add or update item is ended.<br><br>So the ItemAdding and ItemUpdating works always, because it is not a asynchronous request.<br><br>When we test in our console application to add or updating list items everything works fine, when we want to go to production it doesn't work.<br><br>The difference was, in the tests the last statement was a System.Console.ReadLine, which seems to give the enviroment enough time to call the ItemAdded-Event. <br><br>In production we removed that line and the ItemAdded was not called.<br><br>Greetings JJR<hr size="1" align="left" width="25%">welcome to the blues ;-)Fri, 05 Sep 2008 22:38:52 Z2008-09-05T22:38:52Zhttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#039a06e6-5af4-46a2-9424-2a2152d6ae3ahttp://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/5405c69e-edda-4988-9976-3827256762f6#039a06e6-5af4-46a2-9424-2a2152d6ae3aTomKrebshttp://social.msdn.microsoft.com/Profile/en-US/?user=TomKrebsWhy the event handler is not trigged from console app ? <strong>In Production</strong> (or in Dev) I used the below code in the console app which resolved the issue, now the changes made to the SharePoint List from the C# Console App are causing the Event Handler to get kicked:<br/><br/><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"> <p>using <span style="font-size:x-small">System.Threading;<br/><span style="font-size:x-small;color:#2b91af"><span style="font-size:x-small;color:#2b91af">Thread</span></span><span style="font-size:x-small">.Sleep(xyz);   <br/><span style="font-size:x-small;color:#008000"><span style="font-size:x-small;color:#008000"><br/>//xyz milli seconds - setting this to 1 sec (1000) didn't help me, setting to 30 secs (30000) did help, guess this time should be more than the time it takes for the event handler to 'complete'</span></span></span> </span></p> </span></span></span></span></span><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><font size=2 color="#0000ff"> <p> </p> </font></span></span></span></span> <p> </p> </span></span></span><span style="font-size:x-small">The issue is that the console app is immediately getting closed/completed and not giving &quot;enough time&quot; for the event handler to get kicked (and complete), and I find the Sleep statement coming to the rescue.<br/><br/>Thanks JakeJBlues for the hint.</span> <p> </p> </span></span><span style="font-size:x-small"> <p><span style="background:yellow"><br/></span></p> </span>Thu, 16 Jul 2009 13:28:57 Z2009-07-16T13:32:57Z