0

I have developed an EventReceiver(VS2010) i.e. ItemAdded event for SharePoint Online 2010 Custom List. I am creating a subsite when an Item has been added.

But ONLY sometimes event is firing twice. Its not happening all the time, but only sometimes its firing twice.

I verified the event handler and it is registered only once. When I am not creating site in the event handler, I am not having an issue.

I have been looking to fix this issues for the last one week and I could not figure out why its firing twice sometimes.

I appreciate any help on helping me in fixing the issue or any advice

Thanks- TechSource

1 Answer 1

1

Event receivers seems to have this problem. Best option is to go with custom Workflow and make it fire on Item Added. Internally if you are updating the corresponding list, then you may have to DISABLE event firing first & the use listItem.SystemUpdate()

You can use below code snippet:

 private void OnAddedActivated_Invoked(object sender, ExternalDataEventArgs e)
    {
            SPListItem wfItem = this.workflowProperties.Item;
//Here you can call the CreateSubSite();
            wfItem["SiteCreated"] = "YES"; //Set a flag if requires
            UpdateWorkFlowItem(true);
    }

Helper Method:

private void UpdateWorkFlowItem(bool bisDisableEventFiring)
        {
            try
            {
                if (bisDisableEventFiring)
                {
                    SPUtility.ValidateFormDigest();
                    workflowProperties.Web.AllowUnsafeUpdates = true;
                    WorkFlowEventFiring objEventFiring = new WorkFlowEventFiring();
                    objEventFiring.Disable();
                    workflowProperties.Item.SystemUpdate();
                    objEventFiring.Enable();
                    workflowProperties.Web.AllowUnsafeUpdates = false;
                }
                else
                {
                    workflowProperties.Item.SystemUpdate();
                }
            }
            catch (Exception ex)
            {

            }
        }

Helper Class

public class WorkFlowEventFiring : SPItemEventReceiver
    {
        public WorkFlowEventFiring()
        {
        }
        public void Disable()
        {
            this.EventFiringEnabled = false;
        }
        public void Enable()
        {
            this.EventFiringEnabled = true;
        }
    }
1
  • Anand,Thanks for your detailed explanation with code. Thanks
    – user17090
    May 21, 2013 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.