Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 2 down vote accepted

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
share|improve this answer
    
Thanks you're an angel it was called in two features –  Glolita Mar 14 '13 at 13:45

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

share|improve this answer
    
thanks but I didn't write this code in the event handler rather in a user control for a webpart development. –  Glolita Mar 14 '13 at 13:43

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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