0

I am programmatically trying to update an item in Document library. And I have an Event Receiver on Document library on ItemUpdated event. But somehow it is not firing.

Below is my code:

using (SPSite spsite = new SPSite(url))
{
    using (SPWeb spwebs = spsite.OpenWeb())
    {
        SPList spListDisc = spwebs.Lists.TryGetList("listname");
        if (spListDisc != null)
        {
            SPListItemCollection splistColl = spListDisc .GetItems(query);
            foreach (SPListItem item in splistColl)
            {
                spwebs.AllowUnsafeUpdates = true;

                item ["Title"] = title;
                item .Update();
                spwebs.AllowUnsafeUpdates = false;
            }       
        }       
    }
}

Any help would be appreciated.

Thanks.

  • Does it fire when updating an item from the standard UI? Would you share your code (the one that does the update, and the ER)? – Evariste Feb 18 '16 at 9:55
  • Which version of SharePoint and Visual Studio you are using? Also provide content of Event Receiver XML file. – Amit Bhagat Feb 18 '16 at 10:07
  • @Evariste - Yes it fires from the standard UI – DRS Feb 18 '16 at 10:34
  • can you provide your code of ItemUpdated event? – Hardik Feb 18 '16 at 11:10
3

As the Event Receiver works fine when triggered from the UI -> there's no problem with the ER registration itself.
The code that does the Update looks "OK" (not "perfect", a few improvements can be done, like calling spwebs.AllowUnsafeUpdates = true only once, before the loop), and spwebs.AllowUnsafeUpdates = false; is actually not needed since spwebs is disposed right after the end of the loop. But, OK, no big problem there.

The first conclusion I can come to at the moment is: your ER fails in the case it's fired from the code. So, we need the code of the ER.
One thing I've seen quite often: you may assume, in the ER code, that there's a Web context (e.g. a call to HTTPContext.Current)... but there's no Web context if your Update code is called from, let's say, a console app (or a job, or at speficic steps of a workflow...).

So, one more question (in addition to the ER code): where is your Update code called from? Console App? WinForm? Job? Workflow?...

EDIT
OK, thanks to the comments below, I now have a clear picture of what happens: you actually disable event firing (this.EventFiringEnabled = false). This piece of code disables all events on all lists. What it does internally is setting a flag on the thread to disable events. It means all events fired in the same thread are disabled for the life of the thread or till this.EventFiringEnabled = true.

  • Code is on both the events ItemAdded and ItemUpdating and this Event Receiver is on SharePoint List and through this Event Receiver I am updating the SharePoint Document Library;s Title field as mentioned in my code. – DRS Feb 18 '16 at 12:44
  • Are you sure you don't disable event firing at the begin of your ER? Can you please share the ER code with us? – Evariste Feb 18 '16 at 13:02
  • Yes i have disable but as long as i know that would not effect the document library Title's field. As my Event Receiver's code is on the list so it would affect to the list item and not on the document library's item. public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdded(properties); this.EventFiringEnabled = false; UpdateItem(properties); // method to update in Library this.EventFiringEnabled = true; } – DRS Feb 18 '16 at 13:23
  • this.EventFiringEnabled = false will definitely disable all events on all lists! – Evariste Feb 18 '16 at 13:31
  • Actually i have an Event Receiver in list say "List1" and in that i have two events ItemAdded and ItemUpdated. Both events contains the code for updating the item in Document Library say "DocLib1". When i update or add the item in "List1" then it will update the Title in "DocLib1".Now i have put this.EventFiringEnabled = false in "List1" events ItemAdded and ItemUpdated. this.EventFiringEnabled = false, means it will affect that particular item which i have added or updated in that list "List1". It doesnt have any concern to "DocLib1". – DRS Feb 19 '16 at 5:12
0

try this ,

public override void ItemUpdating(SPItemEventProperties properties)
 {
 string fieldInternalName = properties.List.Fields["Source"].InternalName;
  properties.AfterProperties[fieldInternalName] = properties.ListTitle;
 }

https://msdn.microsoft.com/en-us/library/office/gg981880(v=office.14).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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