private async Task CreateSharepointDynamicsLink(MicrosoftDynamicsCRMadoxioWorker worker) { // create a SharePointDocumentLocation link var folderName = worker.GetDocumentFolderName(); //var name = worker.AdoxioWorkerid + " Files"; _fileManagerClient.CreateFolderIfNotExist(_logger, WorkerDocumentUrlTitle, folderName); _dynamicsClient.CreateEntitySharePointDocumentLocation("worker", worker.AdoxioWorkerid, folderName, folderName); }
/// <summary> /// Internal implementation of upload attachment /// </summary> /// <param name="entityId"></param> /// <param name="entityName"></param> /// <param name="file"></param> /// <param name="documentType"></param> /// <param name="checkUser"></param> /// <returns></returns> private async Task <IActionResult> UploadAttachmentInternal(string entityId, string entityName, IFormFile file, string documentType, bool checkUser) { FileSystemItem result = null; if (string.IsNullOrEmpty(entityId) || string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(documentType)) { return(BadRequest()); } var hasAccess = true; if (checkUser) { ValidateSession(); hasAccess = await CanAccessEntity(entityName, entityId).ConfigureAwait(true); } if (!hasAccess) { return(new NotFoundResult()); } // Sanitize file name var illegalInFileName = new Regex(@"[#%*<>?{}~¿""]"); var fileName = illegalInFileName.Replace(file.FileName, ""); illegalInFileName = new Regex(@"[&:/\\|]"); fileName = illegalInFileName.Replace(fileName, "-"); fileName = FileSystemItemExtensions.CombineNameDocumentType(fileName, documentType); var folderName = await _dynamicsClient.GetFolderName(entityName, entityId).ConfigureAwait(true); _dynamicsClient.CreateEntitySharePointDocumentLocation(entityName, entityId, folderName, folderName); var ms = new MemoryStream(); file.OpenReadStream().CopyTo(ms); var data = ms.ToArray(); // call the web service var uploadRequest = new UploadFileRequest { ContentType = file.ContentType, Data = ByteString.CopyFrom(data), EntityName = entityName, FileName = fileName, FolderName = folderName }; var uploadResult = _fileManagerClient.UploadFile(uploadRequest); var logFolderName = WordSanitizer.Sanitize(folderName); var logFileName = WordSanitizer.Sanitize(fileName); if (uploadResult.ResultStatus == ResultStatus.Success) { // Update modifiedon to current time UpdateEntityModifiedOnDate(entityName, entityId, true); _logger.LogInformation($"SUCCESS in uploading file {logFileName} to folder {logFolderName}"); } else { _logger.LogError($"ERROR in uploading file {logFileName} to folder {logFolderName}"); throw new Exception($"ERROR in uploading file {logFileName} to folder {logFolderName}"); } return(new JsonResult(result)); }