1
votes

I have written a program that writes multiple files into a SharePoint-List using CSOM:

foreach (file in myFiles) {
  UploadFileToSharePoint(file.dataq, context, url+ file.name);
}
UploadFileToSharePoint(dummy, context, url+"finish.txt");
     public static void UploadFileToSharePoint(Byte[] fileStream, ClientContext cCtx, String destUrl)
        {
             using (MemoryStream stream = new MemoryStream(fileStream))
            {
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(cCtx, destUrl, stream, true);
            }
        }

As you can see, when done uploading N files into a target folder, a last file called "finish.txt" is uploaded. Now I have an Event Receiver for that list checking if the last file is uploaded:

public override void ItemAdded(SPItemEventProperties properties)
        {
            try
            {
                if (properties.ListItem.FileSystemObjectType == SPFileSystemObjectType.File)
                {
                    if (properties.ListItem.File != null && properties.ListItem.File.ParentFolder != null)
                    {
                        if (properties.ListItem.File.Name.Contains("finish.txt"))
                        {
                            // last file inside folder
                            AnalyzeFolder(properties.ListItem);
                        }
                    }
                }
                base.ItemAdded(properties);
            }

Occasionally I receive an error

Error moving files: Microsoft.SharePoint.SPException: Save Conflict. Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes. ---> System.Runtime.InteropServices.COMException: Save Conflict. Your changes conflict with those made concurrently by another user.

My guess would be that this happens because the last file is uploaded before the uploads of the other files before are finished.

How can I make sure that all uploads are finished before I upload the last file to prevent that error?

1

1 Answers

0
votes

This isn't exactly what I was wanting, but making the Event Receiver work synchronous by adding

<Synchronization>Synchronous</Synchronization>

to the elements.xml solved that issue.

(I won't mark this as an answer, because originally I did not want to have that event receiver synchronously, but to wait for the other events. So if someone has a better answer: Feel free to post it)