locked
ItemCheckedIn event called twice? RRS feed

  • Question

  • Hi, I have an event handler on my document library that is called twice when a document is checked in or uploaded.

    Why is this event handler called twice?  I have a problem because Im triggering an workflow when someone checks in a document and at this point the workflow will be triggered twice...

    here is my code:

     SPWorkflowManager wfmanager = properties.ListItem.ParentList.ParentWeb.Site.WorkflowManager;
                         SPWorkflowAssociation wf = properties.ListItem.ParentList.WorkflowAssociations.GetAssociationByName(WorkflowName, System.Globalization.CultureInfo.CurrentCulture);

                         if (wf != null)
                         {
                            bool isCurrentlyRunning = false;
                            SPWorkflowCollection wfRunningCollection = wfmanager.GetItemActiveWorkflows(properties.ListItem);

                            foreach (SPWorkflow runningWorkflow in wfRunningCollection)
                            {
                               if (runningWorkflow.ParentAssociation.BaseId == wf.BaseId)
                               {
                                  isCurrentlyRunning = true;
                               }
                            }

                            try
                            {
                               if (!isCurrentlyRunning)
                               {
                                 
                                  wfmanager.StartWorkflow(properties.ListItem, wf, wf.AssociationData, true);
                               }
                            }
                            catch (Exception ex)
                            {
                               properties.Cancel = true;
                               properties.ErrorMessage = "Failed to start approval workflow. ";
                               SPSecurity.RunWithElevatedPrivileges(delegate { EventLog.WriteEntry("Knowledge works event handler", string.Concat(new object[] { "Exception in Function: ", ex.Message, " Inner Excpetion: ", ex.InnerException, "Call Stack: ", ex.StackTrace }), EventLogEntryType.Error); });
                            }
                         }
                           
                        
                      }
    Tuesday, November 24, 2009 10:04 AM

Answers

  • It you feel that the ItemCheckedIn event called twice, it is possible that the event handler is deployed twice.
    You can reference the following *SAMPLE * code to iterate the event handlers and remove the duplicate one.

        using (SPSite site = new SPSite("http://server/"))
        {
            using (SPWeb web = site.RootWeb)
            {
                SPList list = web.Lists["List_Name"];
                SPEventReceiverDefinition erd = null ;
     
                foreach (SPEventReceiverDefinition spERD in list.EventReceivers)
                {
                    //Iteration; Get the duplicate one.
                }
                //Remove it
                erd.Delete();
            }
        }
    Cogito ergo sum.
    • Marked as answer by Chengyi Wu Wednesday, December 2, 2009 2:57 AM
    Thursday, November 26, 2009 1:19 AM

All replies

  • So no one can help me with this issue...
    Tuesday, November 24, 2009 12:12 PM
  • It you feel that the ItemCheckedIn event called twice, it is possible that the event handler is deployed twice.
    You can reference the following *SAMPLE * code to iterate the event handlers and remove the duplicate one.

        using (SPSite site = new SPSite("http://server/"))
        {
            using (SPWeb web = site.RootWeb)
            {
                SPList list = web.Lists["List_Name"];
                SPEventReceiverDefinition erd = null ;
     
                foreach (SPEventReceiverDefinition spERD in list.EventReceivers)
                {
                    //Iteration; Get the duplicate one.
                }
                //Remove it
                erd.Delete();
            }
        }
    Cogito ergo sum.
    • Marked as answer by Chengyi Wu Wednesday, December 2, 2009 2:57 AM
    Thursday, November 26, 2009 1:19 AM