0

I am moving a document in the ItemUpdated event from one library to another. I want to fire the ItemAdded event on the newly moved item in the second library.
I've tried to do what I need in the ItemUpdated after the 'move' command, but the item doesn't exist in the new library until after the event receiver completes. My attempts have been like this:

Attempt 1

ItemUpdated()
    **move item to Library2**

    **try to access new item <--Does not work.  below is code-snippet**

    {
    //get sub folder url
    string SubFolderUrl = currWeb.ServerRelativeUrl + "/" + NewLibrary + "/" + folderPath;

   //able to get the subfolder at this point
   SPFolder SubFolder = currWeb.GetFolder(SubFolderUrl);

   //create an SPQuery that gets us what we want
   string fileName = currentItemName;
   SPQuery query = new SPQuery();
   query.Query = "<Where><Contains><FieldRef Name=\"Title\" /><Value Type=\"Text\">" + fileName + "</Value></Contains></Where>";
   query.Folder = SubFolder;  // This should restrict the query to the subfolder

   //run the SPQuery against the SPList
   SPDocumentLibrary list = SubFolder.DocumentLibrary;
   SPListItemCollection file = list.GetItems(query);
   foreach (SPListItem movedItem in file)
    {
       movedItem.CopyTo(destinationLocation.Replace(NewLibrary, OriginalLibrary));
    }

Attempt 2

move item to Library2
*itemAdded event doesn't fire

Aside from creating a second event receiver for itemAdded, any suggestions would be appreciated.

  • What happens in the attempt1 when you try to access to new item created? Could you post more information like code of the event recievers, error log etc. – Unnie Apr 18 '18 at 14:05
  • I use a caml query to get the items from the second location and the count comes back with 0 – Doug Punchak Apr 18 '18 at 14:08
  • Did you check manually by going to the other library whether the document was moved? Also post the code here so that others can verify – Unnie Apr 18 '18 at 14:09
  • I updated my post with the code that runs after the document move. The document is not in the new library when i put a breakpoint right before this. It's only there after the event receiver completes. – Doug Punchak Apr 18 '18 at 14:14

Your Answer

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

Browse other questions tagged or ask your own question.