public async Task <IActionResult> Document([FromForm] DocumentRequest file) { if (file == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(file)); } try { // Save Blob using (var stream = file.File.OpenReadStream()) { var url = await _azureStorageService.SaveDocumentAsync(stream, file.File.FileName, file.DocumentCategoryId); if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentException("Unable to save file."); } var document = _mapper.Map <DocumentRequest, Document>(file); document.CreatedBy = "Share Point"; document.CreatedDate = DateTime.UtcNow; document.LastModified = DateTime.UtcNow; document.LastModifiedBy = "Share Point"; document.DocumentName = file.DocumentName; document.FileName = file.File.FileName; _documentRepository.SetDocument(document); foreach (var partnerType in file.PartnerTypes.Split(',')) { var pd = new PartnerTypeDocument(); pd.PartnerTypeId = int.Parse(partnerType); pd.DocumentId = document.DocumentId; document.PartnerTypeDocuments.Add(pd); } _documentRepository.Complete(); return(CreatedAtRoute("GetDocumentById", new { documentId = document.DocumentId }, file)); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public async Task <IActionResult> UpdateDocument(int documentId, [FromBody] DocumentUpdateRequest file) { if (file == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest()); } var document = _documentRepository.GetById <Document>(documentId); if (document == null) { return(NotFound(documentId)); } try { // remove var types = _documentRepository.GetAll <PartnerTypeDocument>(p => p.DocumentId == documentId); foreach (var type in types) { _documentRepository.Remove <PartnerTypeDocument>(type); } foreach (var partnerType in file.PartnerTypes.Split(',')) { var pd = new PartnerTypeDocument(); pd.PartnerTypeId = int.Parse(partnerType); pd.DocumentId = document.DocumentId; document.PartnerTypeDocuments.Add(pd); } document.DocumentCategoryId = file.DocumentCategoryId; document.DocumentName = file.DocumentName; document.Details = file.Details; document.LastModified = DateTime.UtcNow; document.LastModifiedBy = "Share Point"; _documentRepository.Attach <Document>(document); _documentRepository.Complete(); return(NoContent()); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public async void InsertData() { try { var oListData = ClientContext.Web.Lists.GetByTitle("Aftaler & dokumenter"); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View Scope='RecursiveAll'><Query></Query></View>"; ListItemCollection oListDataItem = oListData.GetItems(camlQuery); ClientContext.Load(oListDataItem); ClientContext.Load(oListDataItem, t => t.Include(p => p.Id, p => p.ContentType, p => p.DisplayName, p => p.FieldValuesAsText)); ClientContext.ExecuteQuery(); log.Info($"Aftaler & dokumenter List data pulled successfully."); foreach (ListItem oItem in oListDataItem) { DocumentCategory category; List <PartnerType> partnerTypes = new List <PartnerType>(); try { log.Info($"Document data processing started Id :{oItem.GetId()}"); DocumentMetaData doc = new DocumentMetaData() { CreatedBy = oItem.GetAuthor(), CreatedDate = oItem.GetCreateDate(), LastModifiedBy = oItem.GetEditor(), LastModifiedDate = oItem.GetModifiedDate(), }; doc.ExpirationDate = oItem.GetModifiedDate(); if (oItem["RelatedPartnerType"] != null) { var childIdField = oItem["RelatedPartnerType"] as FieldLookupValue[]; if (childIdField != null) { foreach (var lookupValue in childIdField) { if (!DKBSDbContext.PartnerType.ToList().Exists(p => p.PartnerTypeTitle.ToLower() == lookupValue.LookupValue.ToLower())) { var partnerType = new PartnerType() { PartnerTypeTitle = lookupValue.LookupValue, CreatedBy = doc.CreatedBy, CreatedDate = doc.CreatedDate, LastModified = doc.LastModifiedDate, LastModifiedBy = doc.LastModifiedBy }; DKBSDbContext.PartnerType.Add(partnerType); DKBSDbContext.SaveChanges(); partnerTypes.Add(partnerType); } else { var partnerType = DKBSDbContext.PartnerType.FirstOrDefault(p => p.PartnerTypeTitle.ToLower() == lookupValue.LookupValue.ToLower()); if (partnerType != null) { partnerTypes.Add(partnerType); } } } } } ClientContext.Load(oItem.ContentType); ClientContext.ExecuteQuery(); if (!DKBSDbContext.DocumentCategory.ToList().Exists(p => p.CategoryName.ToLower() == oItem.ContentType.Name.ToLower())) { category = new Domain.DocumentCategory { CategoryName = oItem.ContentType.Name, CreatedBy = doc.CreatedBy, CreatedDate = doc.CreatedDate, LastModified = doc.LastModifiedDate, LastModifiedBy = doc.LastModifiedBy }; DKBSDbContext.DocumentCategory.Add(category); DKBSDbContext.SaveChanges(); } else { category = DKBSDbContext.DocumentCategory.FirstOrDefault(p => p.CategoryName.ToLower() == oItem.ContentType.Name.ToLower()); } if (oItem.FileSystemObjectType == FileSystemObjectType.File) { Microsoft.SharePoint.Client.File lfile = oItem.File; ClientContext.Load(lfile); ClientContext.ExecuteQuery(); doc.FileName = oItem.File.Name; log.Info($"Document uploading started FileName :{doc.FileName}"); var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ClientContext, lfile.ServerRelativeUrl); var url = await _storageService.SaveDocumentAsync(fileInfo.Stream, doc.FileName, category.DocumentCategoryId); log.Info($"Document uploaded sucessfully. started FileName :{doc.FileName} Category: {oItem.ContentType.Name}"); } var document = new Document() { DocumentCategoryId = category.DocumentCategoryId, ExpirationDate = doc.ExpirationDate, CreatedBy = doc.CreatedBy, CreatedDate = doc.CreatedDate, LastModified = doc.LastModifiedDate, LastModifiedBy = doc.LastModifiedBy, Details = doc.Detail, DocumentName = doc.FileName, FileName = doc.FileName }; foreach (var partnerType in partnerTypes) { var partnerTypeDoc = new PartnerTypeDocument(); partnerTypeDoc.DocumentId = document.DocumentId; partnerTypeDoc.PartnerTypeId = partnerType.PartnerTypeId; document.PartnerTypeDocuments.Add(partnerTypeDoc); } DKBSDbContext.Document.Add(document); DKBSDbContext.SaveChanges(); log.Info($"Document upload successfully Name :{doc.FileName}"); } catch (Exception ex) { ErrorCount += 1; log.Error($"Unable to upload file. Id:{oItem.GetId()}", ex); } } } catch (Exception ex) { ErrorCount += 1; log.Error("Unable to get documents data from sharepoint.", ex); } log.Info($"Total ErrorCount:{ErrorCount}"); }