Sharepoint 2013- update document metadata using itemupdating or itemupdated event is not working

StackOverflow https://stackoverflow.com/questions/20622751

  •  02-09-2022
  •  | 
  •  

Question

I am trying to update custom metadata for a document when document is added to the library using item updated event, but it is not working. A custom aspx application is using a href element to point to the URL of the document. Click it on it opens windows explorer view similar to the one that is OOB sharepoint 2013 explorer view. Now when user copy the document from library1(says lives in site1 in sitecollection1) to library2(lives in site2 in sitecollection2) via copy-paste option, I need to clear some of the metadata of the document. I am trying the Lukasz's suggestion for it, but the metadata is not clearing. In the debug mode, even though event firing is disabled before update, I see the updated event is being called again one more time which is strange. At the end, my metadata is not being cleared. I tried with both updating and updated event. Any idea? here is my code for updated:

public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
            ClearNotes(properties);
        }

 private void ClearNotes(SPItemEventProperties properties)
        {
            try
            {
                SPListItem listItem = properties.ListItem;


                listItem["Notes1"] = string.Empty;
                listItem["ReviewNote"] = null;


                base.EventFiringEnabled = false;
                listItem.Update(); 
            }
            catch (Exception ex)
            {
               //logging error to db
            }
            finally
            {
                base.EventFiringEnabled = true;
            }
        }
Was it helpful?

Solution

I think it must be this.EventFiringEnable = false; Not base. ...

You can also do it with itemupdating and Afterproperties, then you dont need to disable the EventFiring and need no update:

public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);

            ///...
            properties.AfterProperties["Notes1"] = string.Empty;            
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top