Ejemplo n.º 1
0
        public async Task <ActionResult> FileUpload([FromForm] IFormFile file)
        {
            _logger.LogError("Testing File Upload");

            bool documentLibraryExists = await _sharePointFileManager.DocumentLibraryExists(DocumentListTitle);

            if (!documentLibraryExists)
            {
                await _sharePointFileManager.CreateDocumentLibrary(DocumentListTitle, DocumentUrlTitle);

                _logger.LogInformation($"Successfully created document library {DocumentListTitle} on SharePoint");
            }
            else
            {
                _logger.LogInformation($"Successfully retrieved document library {DocumentListTitle} on SharePoint");
            }

            // format file name
            string fileName = SanitizeFileName(file.FileName);

            // upload to SharePoint

            // in this example we are passing null for the folder name, which results in the file being stored directly in the document library.
            // Normally in a situation where you have Dynamics document management setup, you would construct a folder name using the same pattern as
            // used in the configuration of Dynamics Document Management.

            await _sharePointFileManager.AddFile(DocumentListTitle, null, fileName, file.OpenReadStream(), file.ContentType);

            _logger.LogInformation("Successfully uploaded file {FileName} to SharePoint", fileName);

            return(Ok("File uploaded."));
        }
        private async void CreateDocumentLibraryIfMissing(string listTitle, string documentTemplateUrl = null)
        {
            var exists = await _sharePointFileManager.DocumentLibraryExists(listTitle);

            if (!exists)
            {
                await _sharePointFileManager.CreateDocumentLibrary(listTitle, documentTemplateUrl);
            }
        }
        private void CreateDocumentLibraryIfMissing(string listTitle, string documentTemplateUrl = null)
        {
            SharePointFileManager _sharePointFileManager = new SharePointFileManager(_configuration);
            var exists = _sharePointFileManager.DocumentLibraryExists(listTitle).GetAwaiter().GetResult();

            if (!exists)
            {
                _sharePointFileManager.CreateDocumentLibrary(listTitle, documentTemplateUrl).GetAwaiter().GetResult();
            }
        }
        private async Task CreateDocumentLibraryIfMissing(string listTitle, string documentTemplateUrl = null)
        {
            SharePointFileManager _sharePointFileManager = new SharePointFileManager(_configuration);
            var exists = await _sharePointFileManager.DocumentLibraryExists(listTitle);

            if (!exists)
            {
                await _sharePointFileManager.CreateDocumentLibrary(listTitle, documentTemplateUrl);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Hangfire job to send an export to SPD.
        /// </summary>
        public async Task ReceiveImportJob(PerformContext hangfireContext)
        {
            hangfireContext.WriteLine("Starting Sharepoint Checker Job.");
            _logger.LogError("Starting Sharepoint Checker Job.");

            // If folder does not exist create folder.
            var folderExists = await _sharePointFileManager.DocumentLibraryExists(SharePointDocumentTitle);

            if (!folderExists)
            {
                hangfireContext.WriteLine("No directory found. Creating directory.");
                _logger.LogError("No directory found. Creating directory.");
                await _sharePointFileManager.CreateDocumentLibrary(SharePointDocumentTitle);

                await _sharePointFileManager.CreateFolder(SharePointDocumentTitle, SharePointFolderName);

                hangfireContext.WriteLine("End of Sharepoint Checker Job.");
                _logger.LogError("End of Sharepoint Checker Job.");
            }
            else
            {
                List <FileSystemItem> fileSystemItemVMList = await getFileDetailsListInFolder(hangfireContext);

                // Look for files with unprocessed name
                hangfireContext.WriteLine("Looking for unprocessed files.");
                _logger.LogError("Looking for unprocessed files.");
                var unprocessedFiles = fileSystemItemVMList.Where(f => !f.name.StartsWith("processed_")).ToList();
                foreach (var file in unprocessedFiles)
                {
                    // Skip if file is not .csv
                    if (Path.GetExtension(file.name) != ".csv")
                    {
                        continue;
                    }

                    // Download file
                    hangfireContext.WriteLine("File found. Downloading file.");
                    _logger.LogError("File found. Downloading file.");
                    byte[] fileContents = await _sharePointFileManager.DownloadFile(file.serverrelativeurl);

                    // Update worker
                    hangfireContext.WriteLine("Updating worker.");
                    _logger.LogError("Updating worker.");
                    string data = System.Text.Encoding.Default.GetString(fileContents);
                    List <WorkerResponse> parsedData = WorkerResponseParser.ParseWorkerResponse(data, _logger);

                    foreach (var spdResponse in parsedData)
                    {
                        try
                        {
                            await UpdateSecurityClearance(hangfireContext, spdResponse, spdResponse.RecordIdentifier);
                        }
                        catch (SharePointRestException spre)
                        {
                            hangfireContext.WriteLine("Unable to update security clearance status due to SharePoint.");
                            hangfireContext.WriteLine("Request:");
                            hangfireContext.WriteLine(spre.Request.Content);
                            hangfireContext.WriteLine("Response:");
                            hangfireContext.WriteLine(spre.Response.Content);

                            _logger.LogError("Unable to update security clearance status due to SharePoint.");
                            _logger.LogError("Request:");
                            _logger.LogError(spre.Request.Content);
                            _logger.LogError("Response:");
                            _logger.LogError(spre.Response.Content);
                            continue;
                        }
                        catch (Exception e)
                        {
                            hangfireContext.WriteLine("Unable to update security clearance status.");
                            hangfireContext.WriteLine("Message:");
                            hangfireContext.WriteLine(e.Message);

                            _logger.LogError("Unable to update security clearance status.");
                            _logger.LogError("Message:");
                            _logger.LogError(e.Message);
                            continue;
                        }
                    }

                    // Rename file
                    hangfireContext.WriteLine("Finished processing file.");
                    _logger.LogError($"Finished processing file {file.serverrelativeurl}");
                    _logger.LogError($"{parsedData.Count} records updated.");

                    string newserverrelativeurl = "";
                    int    index = file.serverrelativeurl.LastIndexOf("/");
                    if (index > 0)
                    {
                        newserverrelativeurl = file.serverrelativeurl.Substring(0, index);

                        // tag cases where the files were empty.
                        if (parsedData.Count == 0)
                        {
                            newserverrelativeurl += "/" + "processed_empty_" + file.name;
                        }
                        else
                        {
                            newserverrelativeurl += "/" + "processed_" + file.name;
                        }
                    }

                    try
                    {
                        await _sharePointFileManager.RenameFile(file.serverrelativeurl, newserverrelativeurl);
                    }
                    catch (SharePointRestException spre)
                    {
                        hangfireContext.WriteLine("Unable to rename file.");
                        hangfireContext.WriteLine("Request:");
                        hangfireContext.WriteLine(spre.Request.Content);
                        hangfireContext.WriteLine("Response:");
                        hangfireContext.WriteLine(spre.Response.Content);

                        _logger.LogError("Unable to rename file.");
                        _logger.LogError("Message:");
                        _logger.LogError(spre.Message);
                        throw spre;
                    }
                }
            }
            hangfireContext.WriteLine("End of Sharepoint Checker Job.");
            _logger.LogError("End of Sharepoint Checker Job.");
        }