private async Task <List <ViewModels.FileSystemItem> > getFileDetailsListInFolder(string entityId, string entityName, string documentType) { List <ViewModels.FileSystemItem> fileSystemItemVMList = new List <ViewModels.FileSystemItem>(); ValidateSession(); if (string.IsNullOrEmpty(entityId) || string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(documentType)) { return(fileSystemItemVMList); } try { await CreateDocumentLibraryIfMissing(GetDocumentListTitle(entityName), GetDocumentTemplateUrlPart(entityName)); string folderName = await GetFolderName(entityName, entityId, _dynamicsClient);; // Get the file details list in folder List <Interfaces.SharePointFileManager.FileDetailsList> fileDetailsList = null; SharePointFileManager _sharePointFileManager = new SharePointFileManager(_configuration); try { fileDetailsList = await _sharePointFileManager.GetFileDetailsListInFolder(GetDocumentTemplateUrlPart(entityName), folderName, documentType); } catch (SharePointRestException spre) { _logger.LogError(spre, "Error getting SharePoint File List"); throw new Exception("Unable to get Sharepoint File List."); } if (fileDetailsList != null) { foreach (Interfaces.SharePointFileManager.FileDetailsList fileDetails in fileDetailsList) { ViewModels.FileSystemItem fileSystemItemVM = new ViewModels.FileSystemItem() { // remove the document type text from file name name = fileDetails.Name.Substring(fileDetails.Name.IndexOf("__") + 2), // convert size from bytes (original) to KB size = int.Parse(fileDetails.Length), serverrelativeurl = fileDetails.ServerRelativeUrl, timelastmodified = DateTime.Parse(fileDetails.TimeLastModified), documenttype = fileDetails.DocumentType }; fileSystemItemVMList.Add(fileSystemItemVM); } } } catch (Exception e) { _logger.LogError("Error getting SharePoint File List"); _logger.LogError(e.Message); } return(fileSystemItemVMList); }
/// <summary> /// Copy values from a Dynamics legal entity to a view model. /// </summary> /// <param name="to"></param> /// <param name="from"></param> public static void CopyValues(this MS.FileServices.FileSystemItem to, ViewModels.FileSystemItem from) { to.Name = CombineNameDocumentType(from.name, from.documenttype); to.Size = from.size; to.TimeCreated = from.timecreated; to.TimeLastModified = from.timelastmodified; }
public async Task <IActionResult> GetFileDetailsListInFolder([FromRoute] string accountId, [FromRoute] string documentType) { List <ViewModels.FileSystemItem> fileSystemItemVMList = new List <ViewModels.FileSystemItem>(); // get the current user. string temp = _httpContextAccessor.HttpContext.Session.GetString("UserSettings"); UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp); // check that the session is setup correctly. userSettings.Validate(); var accountGUID = new Guid(accountId); var account = await _dynamicsClient.GetAccountById(accountGUID); if (account != null) { var accountIdCleaned = account.Accountid.ToString().ToUpper().Replace("-", ""); string folderName = $"{account.Name}_{accountIdCleaned}"; // Get the file details list in folder List <FileDetailsList> fileDetailsList = null; try { fileDetailsList = await _sharePointFileManager.GetFileDetailsListInFolder(SharePointFileManager.DefaultDocumentListTitle, folderName, documentType); } catch (SharePointRestException spre) { _logger.LogError("Error getting SharePoint File List"); _logger.LogError("Request URI:"); _logger.LogError(spre.Request.RequestUri.ToString()); _logger.LogError("Response:"); _logger.LogError(spre.Response.Content); throw new Exception("Unable to get Sharepoint File List."); } if (fileDetailsList != null) { foreach (FileDetailsList fileDetails in fileDetailsList) { ViewModels.FileSystemItem fileSystemItemVM = new ViewModels.FileSystemItem(); // remove the document type text from file name fileSystemItemVM.name = fileDetails.Name.Substring(0, fileDetails.Name.IndexOf("__")); // convert size from bytes (original) to KB fileSystemItemVM.size = int.Parse(fileDetails.Length); fileSystemItemVM.timelastmodified = DateTime.Parse(fileDetails.TimeLastModified); fileSystemItemVM.documenttype = fileDetails.DocumentType; fileSystemItemVMList.Add(fileSystemItemVM); } } } else { _logger.LogError("Account not found."); return(new NotFoundResult()); } return(Json(fileSystemItemVMList)); }
private async Task <List <ViewModels.FileSystemItem> > GetListFilesInFolder(string entityId, string entityName, string documentType) { List <ViewModels.FileSystemItem> fileSystemItemVMList = new List <ViewModels.FileSystemItem>(); ValidateSession(); if (string.IsNullOrEmpty(entityId) || string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(documentType)) { return(fileSystemItemVMList); } try { // call the web service var request = new FolderFilesRequest() { DocumentType = documentType, EntityId = entityId, EntityName = entityName, FolderName = await GetFolderName(entityName, entityId, _dynamicsClient) }; var result = _fileManagerClient.FolderFiles(request); if (result.ResultStatus == ResultStatus.Success) { // convert the results to the view model. foreach (var fileDetails in result.Files) { ViewModels.FileSystemItem fileSystemItemVM = new ViewModels.FileSystemItem() { // remove the document type text from file name name = fileDetails.Name.Substring(fileDetails.Name.IndexOf("__") + 2), // convert size from bytes (original) to KB size = fileDetails.Size, serverrelativeurl = fileDetails.ServerRelativeUrl, //timelastmodified = fileDetails.TimeLastModified.ToDateTime(), documenttype = fileDetails.DocumentType }; fileSystemItemVMList.Add(fileSystemItemVM); } } else { _logger.LogError($"ERROR in getting folder files for entity {entityName}, entityId {entityId}, docuemnt type {documentType} "); } } catch (Exception e) { _logger.LogError(e, "Error getting SharePoint File List"); } return(fileSystemItemVMList); }
public static MS.FileServices.FileSystemItem ToModel(this ViewModels.FileSystemItem fileSystemItem) { MS.FileServices.File result = null; if (fileSystemItem != null) { result = new MS.FileServices.File() { Id = fileSystemItem.id, Name = CombineNameDocumentType(fileSystemItem.name, fileSystemItem.documenttype), Size = fileSystemItem.size, TimeCreated = fileSystemItem.timecreated, TimeLastModified = fileSystemItem.timelastmodified }; } return(result); }
public async Task <IActionResult> UploadFile([FromRoute] string id, [FromRoute] string entityName, [FromForm] IFormFile file, [FromForm] string documentType) { if (!string.IsNullOrEmpty(id) && Guid.TryParse(id, out Guid entityId) && !string.IsNullOrEmpty(entityName) && !string.IsNullOrEmpty(documentType)) { ViewModels.FileSystemItem result = null; ValidateSession(); CreateDocumentLibraryIfMissing(GetDocumentListTitle(entityName), GetDocumentTemplateUrlPart(entityName)); if (string.IsNullOrEmpty(entityId.ToString()) || string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(documentType)) { return(BadRequest()); } var hasAccess = await CanAccessEntity(entityName, entityId.ToString()); if (!hasAccess) { return(new NotFoundResult()); } // Update modifiedon to current time UpdateEntityModifiedOnDate(entityName, entityId.ToString()); string fileName = FileSystemItemExtensions.CombineNameDocumentType(file.FileName, documentType); string folderName = await GetFolderName(entityName, entityId.ToString(), documentType); try { await _sharePointFileManager.AddFile(GetDocumentTemplateUrlPart(entityName), folderName, fileName, file.OpenReadStream(), file.ContentType); } catch (SharePointRestException ex) { _logger.LogError("Error uploading file to SharePoint"); _logger.LogError(ex.Response.Content); _logger.LogError(ex.Message); return(new NotFoundResult()); } return(Json(result)); } else { return(BadRequest()); } }
public async Task <IActionResult> UploadFile([FromRoute] string entityId, [FromRoute] string entityName, [FromForm] IFormFile file, [FromForm] string documentType) { ViewModels.FileSystemItem result = null; ValidateSession(); if (string.IsNullOrEmpty(entityId) || string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(documentType)) { return(BadRequest()); } await CreateDocumentLibraryIfMissing(GetDocumentListTitle(entityName), GetDocumentTemplateUrlPart(entityName)); var hasAccess = await CanAccessEntity(entityName, entityId); if (!hasAccess) { return(new NotFoundResult()); } // Update modifiedon to current time UpdateEntityModifiedOnDate(entityName, entityId, true); // Sanitize file name Regex illegalInFileName = new Regex(@"[#%*<>?{}~¿""]"); string fileName = illegalInFileName.Replace(file.FileName, ""); illegalInFileName = new Regex(@"[&:/\\|]"); fileName = illegalInFileName.Replace(fileName, "-"); fileName = FileSystemItemExtensions.CombineNameDocumentType(fileName, documentType); string folderName = await GetFolderName(entityName, entityId, _dynamicsClient); try { await _sharePointFileManager.AddFile(GetDocumentTemplateUrlPart(entityName), folderName, fileName, file.OpenReadStream(), file.ContentType); } catch (SharePointRestException ex) { _logger.LogError("Error uploading file to SharePoint"); _logger.LogError(ex.Response.Content); _logger.LogError(ex.Message); return(new NotFoundResult()); } return(Json(result)); }
/// <summary> /// Convert a given voteQuestion to a ViewModel /// </summary> public static ViewModels.FileSystemItem ToViewModel(this MS.FileServices.FileSystemItem fileSystemItem) { ViewModels.FileSystemItem result = null; if (fileSystemItem != null) { result = new ViewModels.FileSystemItem(); if (fileSystemItem.Id != null) { result.id = fileSystemItem.Id; } result.name = GetDocumentName(fileSystemItem.Name); result.documenttype = GetDocumentType(fileSystemItem.Name); result.size = fileSystemItem.Size; result.timecreated = fileSystemItem.TimeCreated; result.timelastmodified = fileSystemItem.TimeLastModified; } return(result); }
public async Task <IActionResult> UploadFile([FromRoute] string id, [FromForm] IFormFile file, [FromForm] string documentType) { ViewModels.FileSystemItem result = null; // get the LegalEntity. // Adoxio_legalentity legalEntity = null; // get the current user. string temp = _httpContextAccessor.HttpContext.Session.GetString("UserSettings"); UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp); // check that the session is setup correctly. userSettings.Validate(); if (id != null) { var applicationId = Guid.Parse(id); var application = await _dynamicsClient.GetApplicationById(applicationId); if (application == null) { return(new NotFoundResult()); } string fileName = FileSystemItemExtensions.CombineNameDocumentType(file.FileName, documentType); var applicationIdCleaned = application.AdoxioApplicationid.ToString().ToUpper().Replace("-", ""); // Dynamics code for the name is {Code(Licence Type (Licence Type))} - {Business Type(Application)} - {Job Number(Application)} string folderName = $"{application.AdoxioLicenceType.AdoxioCode} - {application.AdoxioApplicant.AdoxioBusinesstype}_{applicationIdCleaned}"; try { await _sharePointFileManager.AddFile(folderName, fileName, file.OpenReadStream(), file.ContentType); } catch (Exception ex) { _logger.LogError(ex.Message); _logger.LogError(ex.StackTrace); return(new NotFoundResult()); } } return(Json(result)); }
public async Task <IActionResult> UploadFile([FromRoute] string entityId, [FromRoute] string entityName, [FromForm] IFormFile file, [FromForm] string documentType) { ViewModels.FileSystemItem result = null; ValidateSession(); if (string.IsNullOrEmpty(entityId) || string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(documentType)) { return(BadRequest()); } var hasAccess = await CanAccessEntity(entityName, entityId); if (!hasAccess) { return(new NotFoundResult()); } // Sanitize file name Regex illegalInFileName = new Regex(@"[#%*<>?{}~¿""]"); string fileName = illegalInFileName.Replace(file.FileName, ""); illegalInFileName = new Regex(@"[&:/\\|]"); fileName = illegalInFileName.Replace(fileName, "-"); fileName = FileSystemItemExtensions.CombineNameDocumentType(fileName, documentType); string folderName = null; folderName = await GetEntitySharePointDocumentationLocation(entityName, entityId); if (folderName == null) { folderName = await GetFolderName(entityName, entityId, _dynamicsClient); } MemoryStream ms = new MemoryStream(); file.OpenReadStream().CopyTo(ms); byte[] 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); if (uploadResult.ResultStatus == ResultStatus.Success) { // Update modifiedon to current time UpdateEntityModifiedOnDate(entityName, entityId, true); _logger.LogInformation($"SUCCESS in uploading file {fileName} to folder {folderName}"); } else { _logger.LogError($"ERROR in uploading file {fileName} to folder {folderName}"); throw new Exception($"ERROR in uploading file {fileName} to folder {folderName}"); } return(new JsonResult(result)); }
public async Task <IActionResult> UploadFile([FromRoute] string entityId, [FromRoute] string entityName, [FromForm] IFormFile file, [FromForm] string documentType) { ViewModels.FileSystemItem result = null; ValidateSession(); if (string.IsNullOrEmpty(entityId) || string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(documentType)) { return(BadRequest()); } await CreateDocumentLibraryIfMissing(GetDocumentListTitle(entityName), GetDocumentTemplateUrlPart(entityName)); var hasAccess = await CanAccessEntity(entityName, entityId); if (!hasAccess) { return(new NotFoundResult()); } // Update modifiedon to current time UpdateEntityModifiedOnDate(entityName, entityId, true); // Sanitize file name Regex illegalInFileName = new Regex(@"[#%*<>?{}~¿""]"); string fileName = illegalInFileName.Replace(file.FileName, ""); illegalInFileName = new Regex(@"[&:/\\|]"); fileName = illegalInFileName.Replace(fileName, "-"); fileName = FileSystemItemExtensions.CombineNameDocumentType(fileName, documentType); string folderName = null; folderName = await GetEntitySharePointDocumentationLocation(entityName, entityId); if (folderName == null) { folderName = await GetFolderName(entityName, entityId, _dynamicsClient); } SharePointFileManager _sharePointFileManager = new SharePointFileManager(_configuration); string headers = LoggingEvents.GetHeaders(Request); try { fileName = await _sharePointFileManager.AddFile(GetDocumentTemplateUrlPart(entityName), folderName, fileName, file.OpenReadStream(), file.ContentType); result = new ViewModels.FileSystemItem() { name = fileName }; _logger.LogInformation($"SUCCESS in uploading file {fileName} to folder {folderName} Headers: {headers} "); } catch (SharePointRestException ex) { _logger.LogError($"ERROR in uploading file {fileName} to folder {folderName} Headers: {headers} - SharePointRestException - {ex.Message} {ex.Response.Content}"); return(new NotFoundResult()); } catch (Exception e) { _logger.LogError($"ERROR in uploading file {fileName} to folder {folderName} Headers: {headers} Unexpected Exception {e.ToString()} {e.Message} {e.StackTrace.ToString()}"); } return(new JsonResult(result)); }