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);
}
In AnalyzeFolder()
I try to move all files from that directory into another.
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?