Microsoft Developer Network > Domovská stránka fór > SharePoint - Development and Programming > Why the event handler is not trigged from console app ?
Odeslat dotazOdeslat dotaz
 

OdpovědětWhy the event handler is not trigged from console app ?

  • 31. srpna 2008 18:26ovulex Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Obsahuje kód
     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

Odpovědi

  • 5. září 2008 22:38JakeJBlues Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    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 ;-)

Všechny reakce

  • 31. srpna 2008 22:55Gary LapointeMVPUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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.
  • 1. září 2008 0:52Moonis TahirMVPUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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.
  • 1. září 2008 3:27ErucyMVPUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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......
  • 1. září 2008 20:14ovulex Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Obsahuje kód
    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
  • 2. září 2008 1:09Gary LapointeMVPUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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?
  • 5. září 2008 22:38JakeJBlues Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    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 ;-)
  • 16. července 2009 13:28TomKrebs Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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.