Question

I'm trying to do some treatement after creating an item but the treatement is done twice because in my code after creating the item, I'm updating it , so the event reciever get fired twice: first when I add the element and the second when I updated it. Here's the code :

if (Mode == OperationMode.New)
{
    item = ListeActions.AddItem();
    item["Client"] = new SPFieldLookupValue(myClient.ID, myClient["Name"] as string).ToString();
    //the Event Reciever ItemAdded is fired here
}
if (Mode == OperationMode.Edit)
{
    item = ListeActions.GetItemById(ActionItemID);
}
TextField ACObject = (TextField)Page.GetControlById("ACObjectID");
UserField ACAssignedTo = (UserField)Page.GetControlById("ACAssignedToID");
NoteField ACDetails = (NoteField)Page.GetControlById("ACDetailsID");

item["Object"] = ACObject.Value;
item["AssignedTo"] = ACAssignedTo.Value;
item["Details"] = ACDetails.Value;

item.Update(); 
//if we're in the case of adding new item then the event
//reciever ItemAdded is fired here for the second time
//while ItemUpdated is not fired

So how can I stop the second fire of itemAdded resulting on the item.Update() yet keep the first fire since I need to keep applying my treatement?

PS: in the case of updating an existing ListeActions.item the itemUpdated is fired twice too !!

BTW : This Code isn't written in the itemadded method nor in the itemupdated method, actually it's the code in a user control for a webpart development.

Was it helpful?

Solution

You probably have your event receiver registered twice. Use PowerShell to find out.

$site = Get-SPSite http://myportal.com
$web = $site.OpenWeb("/path/to/my/site")
$list = $web.Lists["The List"]
$list.EventReceivers | Select Type, Class | Order Type, Class

OTHER TIPS

Disable event firing before you Update your item.

this.EventFiringEnabled = false;
item.Update();
this.EventFiringEnabled = true;

Note that this might cause problems when item.Update() throws an exception, leaving your eventfiring off.. you can use a try/catch to solve this.

hope I could help : )

May i know where was this particular code written? is it on ItemUpdated() or ItemAdded(). There is difference between these two. In ItemAdded() the event receiver will get fired only when the new item is added in the ItemUpdated() the event receiver will get fired whenever the item gets updated no matter on how may times when you update. Also i suggest you to do the following things.

Either in ItemAdded or ItemUpdated you can find this code available by default.

       base.ItemUpdated(properties);
       base.ItemUpdating(properties);
       base.EventFiringEnabled = false;

Comment these code and for updating use the following method and don't use

item.Update(); directly.

Instead use the following method for updating.

           base.EventFiringEnabled = false;
           try
           {
               contractlistitem.SystemUpdate(false);
           }
           finally
           {
               base.EventFiringEnabled = true;
           }

Thanks

For this,

You need to set scope of feature = Web as well as you need to set scope in Feature.Template.XML also.

You can see the solution here. http://asharepointsolutions.blogspot.in/2014/10/sharepoint-event-handler-firing-twice.html

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top