private void CleanupUnprocessedFiles(SharePointDocumentCollection documents) { foreach (SharePointDocument document in documents) { try { File.Delete(Path.Combine(_tempPath, document.FileName)); } catch (IOException ex) { TraceFactory.Logger.Error(ex); } } }
/// <summary> /// Processes existing files in the destination being monitored. /// </summary> private void ProcessExisting() { SharePointDocumentCollection documents = new SharePointDocumentCollection(); SharePointDocumentCollection retrieved = null; int retrievedCount = -1; // Download the documents in batches, to avoid overloading the server do { // Retrieve document information and add it to the master list retrievedCount = -1; //Reset the counter try { retrieved = _library.Retrieve(_query); retrievedCount = retrieved.Count; } catch (WebException webEx) { TraceFactory.Logger.Error(webEx); return; //Unable to connect to the Sharepoint server. } catch (Win32Exception winEx) { TraceFactory.Logger.Error(winEx); return; //Unable to connect to the Sharepoint server. } // Download each document foreach (SharePointDocument document in retrieved) { try { _library.Download(document, _tempPath); documents.Add(document); } catch (WebException webEx) { LogDownloadError(document.FileName, webEx); } catch (NotSupportedException ex) { LogDownloadError(document.FileName, ex); } } // Remove the documents from the server so we don't pick them up again try { _library.Delete(retrieved); } catch (WebException webEx) { TraceFactory.Logger.Error("Error removing downloaded files from Sharepoint. Cleaning up.", webEx); CleanupUnprocessedFiles(documents); return; } // Repeat until we get a partial batch, which means we have downloaded // the remainder of the documents on the server. } while (retrievedCount == _query.DocumentLimit); TraceFactory.Logger.Debug("Downloaded {0} documents from {1}.".FormatWith(documents.Count, _library.Name)); // Process the documents. Note that this step must occur after all the documents are downloaded // so that any metadata files will be present in the directory with their corresponding documents. var selectedDocuments = documents.Where(n => n.FileName.EndsWith(_fileExtension, StringComparison.OrdinalIgnoreCase)); foreach (SharePointDocument document in selectedDocuments) { ProcessDocument(document, Path.Combine(_tempPath, document.FileName)); } }