none
Why the event handler is not trigged from console app ? RRS feed

  • Question

  •  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:

    static void Main(string[] args)
           {
               using (SPSite site = new SPSite("http://ovidiubeches/"))
               {
                   using (SPWeb web = site.OpenWeb("dt"))
                   {
                       web.AllowUnsafeUpdates = true;

                       SPList list = web.Lists["Tasks"];
                       SPListItem item = list.Items.Add();
                       item["Title"] = "new item " + DateTime.Now.ToString();
                       item.Update();

                       web.AllowUnsafeUpdates = false;
                   }
               }
           }

    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 ?

    many thanx,

    Ovidiu
    Sunday, August 31, 2008 6:26 PM

Answers

  • Hello Togehter,

    it seems very funny, we had the same problem.

    The ItemAdded-Event seems not to be fired, when the application, which requests a add or update item is ended.

    So the ItemAdding and ItemUpdating works always, because it is not a asynchronous request.

    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.

    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.

    In production we removed that line and the ItemAdded was not called.

    Greetings JJR
    welcome to the blues ;-)
    Friday, September 5, 2008 10:38 PM

All replies

  • 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.
    Sunday, August 31, 2008 10:55 PM
  • 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.
    If possible if you can paste some ItemAdded event code that will be great.
    Monday, September 1, 2008 12:52 AM
  • 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).
    It cannot trigged in a console application, because it's not running under the "web". I think it's by design......
    Monday, September 1, 2008 3:27 AM
  • Yes Erucy, I think same right now .... but do have somebody have a explanation for this ? 
    Here is the event handler code : ...as I said before it is working perfect in other situations :)

     public class ListSyncEventHandler : SPItemEventReceiver
        {        public override void ItemAdded(SPItemEventProperties properties)
            {
                DisableEventFiring();

                try
                {
                    Logging.LogMessage("info", "start ListSyncEventHandler.ItemAdded");            }
                catch (Exception ex)
                {
                    Logging.LogMessage("Exception", "Exception in ItemAdded");
                    Logging.LogException(ex);
                    throw ex;
                }

                EnableEventFiring();
            }

     .....
    }

    So I do invite other sharepoint programmer to try add items from a custom app console (c#/vb)... not stsadm

    many thanx,

    Ovidiu
    Monday, September 1, 2008 8:14 PM
  • 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?
    Tuesday, September 2, 2008 1:09 AM
  • Hello Togehter,

    it seems very funny, we had the same problem.

    The ItemAdded-Event seems not to be fired, when the application, which requests a add or update item is ended.

    So the ItemAdding and ItemUpdating works always, because it is not a asynchronous request.

    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.

    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.

    In production we removed that line and the ItemAdded was not called.

    Greetings JJR
    welcome to the blues ;-)
    Friday, September 5, 2008 10:38 PM
  • In Production (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:

    using System.Threading;
    Thread.Sleep(xyz);  

    //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'

     

     

    The issue is that the console app is immediately getting closed/completed and not giving "enough time" for the event handler to get kicked (and complete), and I find the Sleep statement coming to the rescue.

    Thanks JakeJBlues for the hint.

     


    • Edited by TomKrebs Thursday, July 16, 2009 1:32 PM
    Thursday, July 16, 2009 1:28 PM