public async void UploadRemoveFilesTest() { Random rnd = new Random(Guid.NewGuid().GetHashCode()); string name = "test-name" + rnd.Next() + ".txt"; string testFolder = "test-folder" + rnd.Next(); string listTitle = "Shared Documents"; string url = serverAppIdUri + "/cannabisdev/Shared Documents/" + testFolder + "/" + name; string contentType = "text/plain"; string testData = "This is just a test."; MemoryStream fileData = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(testData)); await sharePointFileManager.CreateFolder(listTitle, testFolder); await sharePointFileManager.UploadFile(name, "Shared Documents", testFolder, fileData, contentType); // now delete it. await sharePointFileManager.DeleteFile("Shared Documents", testFolder, name); // cleanup the test folder. await sharePointFileManager.DeleteFolder("Shared Documents", testFolder); }
public override Task <CreateFolderReply> CreateFolder(CreateFolderRequest request, ServerCallContext context) { var result = new CreateFolderReply(); var logFolder = WordSanitizer.Sanitize(request.FolderName); var listTitle = GetDocumentListTitle(request.EntityName); var _sharePointFileManager = new SharePointFileManager(_configuration); CreateDocumentLibraryIfMissing(listTitle, GetDocumentTemplateUrlPart(request.EntityName)); var folderExists = false; try { var folder = _sharePointFileManager.GetFolder(listTitle, request.FolderName).GetAwaiter().GetResult(); if (folder != null) { folderExists = true; } } catch (SharePointRestException ex) { Log.Error(ex, $"SharePointRestException creating sharepoint folder (status code: {ex.Response.StatusCode})"); folderExists = false; } catch (Exception e) { Log.Error(e, "Generic Exception creating sharepoint folder"); folderExists = false; } if (folderExists) { result.ResultStatus = ResultStatus.Success; } else { try { _sharePointFileManager.CreateFolder(GetDocumentListTitle(request.EntityName), request.FolderName) .GetAwaiter().GetResult(); var folder = _sharePointFileManager.GetFolder(listTitle, request.FolderName).GetAwaiter() .GetResult(); if (folder != null) { result.ResultStatus = ResultStatus.Success; } } catch (SharePointRestException ex) { result.ResultStatus = ResultStatus.Fail; result.ErrorDetail = $"ERROR in creating folder {logFolder}"; Log.Error(ex, result.ErrorDetail); } }
public override Task <CreateFolderReply> CreateFolder(CreateFolderRequest request, ServerCallContext context) { var result = new CreateFolderReply(); string listTitle = GetDocumentListTitle(request.EntityName); SharePointFileManager _sharePointFileManager = new SharePointFileManager(_configuration); CreateDocumentLibraryIfMissing(listTitle, GetDocumentTemplateUrlPart(request.EntityName)); bool folderExists = false; try { var folder = _sharePointFileManager.GetFolder(listTitle, request.FolderName).GetAwaiter().GetResult(); if (folder != null) { folderExists = true; } } catch (Exception) { folderExists = false; } if (folderExists) { result.ResultStatus = ResultStatus.Success; } else { try { _sharePointFileManager.CreateFolder(GetDocumentListTitle(request.EntityName), request.FolderName).GetAwaiter().GetResult(); var folder = _sharePointFileManager.GetFolder(listTitle, request.FolderName).GetAwaiter().GetResult(); if (folder != null) { result.ResultStatus = ResultStatus.Success; } } catch (SharePointRestException ex) { result.ResultStatus = ResultStatus.Fail; result.ErrorDetail = $"ERROR in creating folder {request.FolderName}"; _logger.LogError(ex, result.ErrorDetail); } catch (Exception e) { result.ResultStatus = ResultStatus.Fail; result.ErrorDetail = $"ERROR in creating folder {request.FolderName}"; _logger.LogError(e, result.ErrorDetail); } } return(Task.FromResult(result)); }
/// <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."); }
private async Task CreateSharepointDynamicsLink(MicrosoftDynamicsCRMadoxioWorker worker) { // create a SharePointDocumentLocation link string folderName = await FileController.GetFolderName("worker", worker.AdoxioWorkerid, _dynamicsClient); string name = worker.AdoxioWorkerid + " Files"; SharePointFileManager _sharePointFileManager = new SharePointFileManager(_configuration); // Create the folder bool folderExists = await _sharePointFileManager.FolderExists(SharePointFileManager.WorkertDocumentUrlTitle, folderName); if (!folderExists) { try { await _sharePointFileManager.CreateFolder(SharePointFileManager.WorkertDocumentUrlTitle, folderName); } catch (Exception e) { _logger.LogError("Error creating Sharepoint Folder"); _logger.LogError($"List is: {SharePointFileManager.WorkertDocumentUrlTitle}"); _logger.LogError($"FolderName is: {folderName}"); throw e; } } // Create the SharePointDocumentLocation entity MicrosoftDynamicsCRMsharepointdocumentlocation mdcsdl = new MicrosoftDynamicsCRMsharepointdocumentlocation() { Relativeurl = folderName, Description = "Worker Qualification", Name = name }; try { mdcsdl = _dynamicsClient.Sharepointdocumentlocations.Create(mdcsdl); } catch (HttpOperationException httpOperationException) { _logger.LogError("Error creating SharepointDocumentLocation"); _logger.LogError("Request:"); _logger.LogError(httpOperationException.Request.Content); _logger.LogError("Response:"); _logger.LogError(httpOperationException.Response.Content); mdcsdl = null; } if (mdcsdl != null) { // add a regardingobjectid. string workerReference = _dynamicsClient.GetEntityURI("adoxio_workers", worker.AdoxioWorkerid); var patchSharePointDocumentLocation = new MicrosoftDynamicsCRMsharepointdocumentlocation(); patchSharePointDocumentLocation.RegardingobjectidWorkerApplicationODataBind = workerReference; // set the parent document library. string parentDocumentLibraryReference = GetDocumentLocationReferenceByRelativeURL(SharePointFileManager.WorkertDocumentUrlTitle); patchSharePointDocumentLocation.ParentsiteorlocationSharepointdocumentlocationODataBind = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", parentDocumentLibraryReference); try { _dynamicsClient.Sharepointdocumentlocations.Update(mdcsdl.Sharepointdocumentlocationid, patchSharePointDocumentLocation); } catch (HttpOperationException httpOperationException) { _logger.LogError("Error adding reference SharepointDocumentLocation to application"); _logger.LogError("Request:"); _logger.LogError(httpOperationException.Request.Content); _logger.LogError("Response:"); _logger.LogError(httpOperationException.Response.Content); throw httpOperationException; } string sharePointLocationData = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", mdcsdl.Sharepointdocumentlocationid); // update the sharePointLocationData. Odataid oDataId = new Odataid() { OdataidProperty = sharePointLocationData }; try { _dynamicsClient.Workers.AddReference(worker.AdoxioWorkerid, "adoxio_worker_SharePointDocumentLocations", oDataId); } catch (HttpOperationException httpOperationException) { _logger.LogError("Error adding reference to SharepointDocumentLocation"); _logger.LogError("Request:"); _logger.LogError(httpOperationException.Request.Content); _logger.LogError("Response:"); _logger.LogError(httpOperationException.Response.Content); throw httpOperationException; } } }
private async Task initializeSharepoint(MicrosoftDynamicsCRMadoxioApplication adoxioApplication) { // create a SharePointDocumentLocation link string folderName = GetApplicationFolderName(adoxioApplication); string name = adoxioApplication.AdoxioJobnumber + " Files"; // Create the folder bool folderExists = await _sharePointFileManager.FolderExists(ApplicationDocumentUrlTitle, folderName); if (!folderExists) { try { await _sharePointFileManager.CreateFolder(ApplicationDocumentUrlTitle, folderName); } catch (SharePointRestException spre) { _logger.LogError("Error creating Sharepoint Folder"); _logger.LogError($"List is: {ApplicationDocumentUrlTitle}"); _logger.LogError($"FolderName is: {folderName}"); _logger.LogError($"Request is: {spre.Request.Content}"); _logger.LogError($"Response is: {spre.Response.Content}"); _logger.LogError($"Error is: {spre.Message} {spre.StackTrace}"); throw spre; } catch (Exception e) { _logger.LogError("Error creating Sharepoint Folder"); _logger.LogError($"List is: {ApplicationDocumentUrlTitle}"); _logger.LogError($"FolderName is: {folderName}"); _logger.LogError($"Error is: {e.Message} {e.StackTrace}"); throw e; } } // Create the SharePointDocumentLocation entity MicrosoftDynamicsCRMsharepointdocumentlocation mdcsdl = new MicrosoftDynamicsCRMsharepointdocumentlocation() { Relativeurl = folderName, Description = "Application Files", Name = name }; try { mdcsdl = _dynamicsClient.Sharepointdocumentlocations.Create(mdcsdl); } catch (OdataerrorException odee) { string mdcsdlId = _dynamicsClient.GetCreatedRecord(odee, null); if (!string.IsNullOrEmpty(mdcsdlId)) { mdcsdl.Sharepointdocumentlocationid = mdcsdlId; } else { _logger.LogError("Error creating SharepointDocumentLocation"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); mdcsdl = null; } } if (mdcsdl != null) { // add a regardingobjectid. string applicationReference = _dynamicsClient.GetEntityURI("adoxio_applications", adoxioApplication.AdoxioApplicationid); var patchSharePointDocumentLocation = new MicrosoftDynamicsCRMsharepointdocumentlocation(); patchSharePointDocumentLocation.RegardingobjectidAdoxioApplicationODataBind = applicationReference; // set the parent document library. string parentDocumentLibraryReference = GetDocumentLocationReferenceByRelativeURL("adoxio_application"); patchSharePointDocumentLocation.ParentsiteorlocationSharepointdocumentlocationODataBind = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", parentDocumentLibraryReference); try { _dynamicsClient.Sharepointdocumentlocations.Update(mdcsdl.Sharepointdocumentlocationid, patchSharePointDocumentLocation); } catch (OdataerrorException odee) { _logger.LogError("Error adding reference SharepointDocumentLocation to application"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); } string sharePointLocationData = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", mdcsdl.Sharepointdocumentlocationid); // update the sharePointLocationData. Odataid oDataId = new Odataid() { OdataidProperty = sharePointLocationData }; try { _dynamicsClient.Applications.AddReference(adoxioApplication.AdoxioApplicationid, "adoxio_application_SharePointDocumentLocations", oDataId); } catch (OdataerrorException odee) { _logger.LogError("Error adding reference to SharepointDocumentLocation"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); } } }
private async Task CreateAccountDocumentLocation(MicrosoftDynamicsCRMaccount account) { string serverRelativeUrl = account.GetServerUrl(_sharePointFileManager); string name = ""; if (string.IsNullOrEmpty(account.BcgovDoingbusinessasname)) { name = account.Accountid; } else { name = account.BcgovDoingbusinessasname; } name += " Account Files"; // create a SharePointDocumentLocation link string folderName = "_" + account.Accountid; // Create the folder bool folderExists = await _sharePointFileManager.FolderExists(SharePointFileManager.AccountDocumentListTitle, folderName); if (!folderExists) { try { var folder = await _sharePointFileManager.CreateFolder(SharePointFileManager.AccountDocumentListTitle, folderName); } catch (Exception e) { _logger.LogError("Error creating Sharepoint Folder"); _logger.LogError($"List is: {SharePointFileManager.AccountDocumentListTitle}"); _logger.LogError($"FolderName is: {folderName}"); throw e; } } // now create a document location to link them. // Create the SharePointDocumentLocation entity MicrosoftDynamicsCRMsharepointdocumentlocation mdcsdl = new MicrosoftDynamicsCRMsharepointdocumentlocation() { Relativeurl = folderName, Description = "Account Files", Name = name }; try { mdcsdl = _dynamicsClient.Sharepointdocumentlocations.Create(mdcsdl); } catch (OdataerrorException odee) { _logger.LogError("Error creating SharepointDocumentLocation"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); mdcsdl = null; } if (mdcsdl != null) { // set the parent document library. string parentDocumentLibraryReference = GetDocumentLocationReferenceByRelativeURL("account"); string accountUri = _dynamicsClient.GetEntityURI("accounts", account.Accountid); // add a regardingobjectid. var patchSharePointDocumentLocationIncident = new MicrosoftDynamicsCRMsharepointdocumentlocation() { RegardingobjectIdAccountODataBind = accountUri, ParentsiteorlocationSharepointdocumentlocationODataBind = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", parentDocumentLibraryReference), Relativeurl = folderName, Description = "Account Files", }; try { _dynamicsClient.Sharepointdocumentlocations.Update(mdcsdl.Sharepointdocumentlocationid, patchSharePointDocumentLocationIncident); } catch (OdataerrorException odee) { _logger.LogError("Error adding reference SharepointDocumentLocation to account"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); } string sharePointLocationData = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", mdcsdl.Sharepointdocumentlocationid); OdataId oDataId = new OdataId() { OdataIdProperty = sharePointLocationData }; try { _dynamicsClient.Accounts.AddReference(account.Accountid, "Account_SharepointDocumentLocation", oDataId); } catch (OdataerrorException odee) { _logger.LogError("Error adding reference to SharepointDocumentLocation"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); } } }
public async Task <IActionResult> CreateApplication([FromBody] ViewModels.AdoxioApplication item) { // for association with current user string userJson = _httpContextAccessor.HttpContext.Session.GetString("UserSettings"); UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(userJson); int count = GetSubmittedCountByApplicant(userSettings.AccountId); if (count >= 8) { return(BadRequest("8 applications have already been submitted. Can not create more")); } MicrosoftDynamicsCRMadoxioApplication adoxioApplication = new MicrosoftDynamicsCRMadoxioApplication(); // copy received values to Dynamics Application adoxioApplication.CopyValues(item); adoxioApplication.AdoxioApplicanttype = (int?)item.applicantType; try { var adoxioLicencetype = _dynamicsClient.GetAdoxioLicencetypeByName(item.licenseType); // set license type relationship adoxioApplication.AdoxioLicenceTypeODataBind = _dynamicsClient.GetEntityURI("adoxio_licencetypes", adoxioLicencetype.AdoxioLicencetypeid); adoxioApplication.AdoxioApplicantODataBind = _dynamicsClient.GetEntityURI("accounts", userSettings.AccountId); adoxioApplication = _dynamicsClient.Applications.Create(adoxioApplication); } catch (OdataerrorException odee) { string applicationId = _dynamicsClient.GetCreatedRecord(odee, null); if (!string.IsNullOrEmpty(applicationId) && Guid.TryParse(applicationId, out Guid applicationGuid)) { adoxioApplication = await _dynamicsClient.GetApplicationById(applicationGuid); } else { _logger.LogError("Error creating application"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); // fail if we can't create. throw (odee); } } // in case the job number is not there, try getting the record from the server. if (adoxioApplication.AdoxioJobnumber == null) { _logger.LogError("AdoxioJobnumber is null, fetching record again."); Guid id = Guid.Parse(adoxioApplication.AdoxioApplicationid); adoxioApplication = await _dynamicsClient.GetApplicationById(id); } if (adoxioApplication.AdoxioJobnumber == null) { _logger.LogError("Unable to get the Job Number for the Application."); throw new Exception("Error creating Licence Application."); } // create a SharePointDocumentLocation link string folderName = GetApplicationFolderName(adoxioApplication); string name = adoxioApplication.AdoxioJobnumber + " Files"; // Create the folder bool folderExists = await _sharePointFileManager.FolderExists(ApplicationDocumentUrlTitle, folderName); if (!folderExists) { try { var folder = await _sharePointFileManager.CreateFolder(ApplicationDocumentUrlTitle, folderName); } catch (Exception e) { _logger.LogError("Error creating Sharepoint Folder"); _logger.LogError($"List is: {ApplicationDocumentUrlTitle}"); _logger.LogError($"FolderName is: {folderName}"); throw e; } } // Create the SharePointDocumentLocation entity MicrosoftDynamicsCRMsharepointdocumentlocation mdcsdl = new MicrosoftDynamicsCRMsharepointdocumentlocation() { Relativeurl = folderName, Description = "Application Files", Name = name }; try { mdcsdl = _dynamicsClient.SharepointDocumentLocations.Create(mdcsdl); } catch (OdataerrorException odee) { string mdcsdlId = _dynamicsClient.GetCreatedRecord(odee, null); if (!string.IsNullOrEmpty(mdcsdlId)) { mdcsdl.Sharepointdocumentlocationid = mdcsdlId; } else { _logger.LogError("Error creating SharepointDocumentLocation"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); mdcsdl = null; } } if (mdcsdl != null) { // add a regardingobjectid. string applicationReference = _dynamicsClient.GetEntityURI("adoxio_applications", adoxioApplication.AdoxioApplicationid); var patchSharePointDocumentLocation = new MicrosoftDynamicsCRMsharepointdocumentlocation(); patchSharePointDocumentLocation.RegardingobjectidAdoxioApplicationODataBind = applicationReference; // set the parent document library. string parentDocumentLibraryReference = GetDocumentLocationReferenceByRelativeURL("adoxio_application"); patchSharePointDocumentLocation.ParentsiteorlocationSharepointdocumentlocationODataBind = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", parentDocumentLibraryReference); try { _dynamicsClient.SharepointDocumentLocations.Update(mdcsdl.Sharepointdocumentlocationid, patchSharePointDocumentLocation); } catch (OdataerrorException odee) { _logger.LogError("Error adding reference SharepointDocumentLocation to application"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); } string sharePointLocationData = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", mdcsdl.Sharepointdocumentlocationid); // update the sharePointLocationData. Odataid oDataId = new Odataid() { OdataidProperty = sharePointLocationData }; try { _dynamicsClient.Applications.AddReference(adoxioApplication.AdoxioApplicationid, "adoxio_application_SharePointDocumentLocations", oDataId); } catch (OdataerrorException odee) { _logger.LogError("Error adding reference to SharepointDocumentLocation"); _logger.LogError("Request:"); _logger.LogError(odee.Request.Content); _logger.LogError("Response:"); _logger.LogError(odee.Response.Content); } } return(Json(await adoxioApplication.ToViewModel(_dynamicsClient))); }