public async Task <IActionResult> Picture([FromForm] PictureRequest file) { if (file == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest()); } try { // Save Blob using (var stream = file.File.OpenReadStream()) { var url = await _azureStorageService.SavePictureAsync(stream, file.File.FileName, file.CRMPartnerId, file.PictureFolderId); if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentException("Unable to save file."); } } var pictureData = _mapper.Map <PictureRequest, Picture>(file); pictureData.CreatedBy = "Share Point"; pictureData.CreatedDate = DateTime.UtcNow; pictureData.PictureName = file.File.FileName; pictureData.Title = file.File.Name; _pictureRepository.SetPicture(pictureData); _pictureRepository.Complete(); return(CreatedAtRoute("GetPicutreById", new { pictureData.PictureId }, pictureData)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public async void InsertData() { var oList = ClientContext.Web.Lists.GetByTitle("Partnere"); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View Scope='Recursive'><Query><ViewFields><FieldRef Name='ID'/><FieldRef Name='ID'/><FieldRef Name='Title'/><FieldRef Name='CISite'/><FieldRef Name='CISiteShortUrl'/></ViewFields></Query></View>"; ListItemCollection collListItem = oList.GetItems(camlQuery); ClientContext.Load(collListItem); ClientContext.ExecuteQuery(); log.Info($"Partnere & dokumenter List data pulled successfully."); foreach (ListItem oListItem in collListItem) { try { var hyperLink = ((FieldUrlValue)(oListItem["CISite"])); log.Info($"Partner CISite Partner Id {oListItem.Id} Title :{oListItem["Title"]}"); if (hyperLink != null) { CRMPartner crmPartner = DKBSDbContext.CRMPartner.FirstOrDefault(p => p.SharePointId == int.Parse(oListItem["ID"].ToString())); if (crmPartner == null) { log.Error($"Partner not found in dkbs sql database Sharepoint Id:{oListItem.GetId()}"); continue; } log.Info($"Started Partner Id {oListItem.Id} Title :{oListItem["Title"]}"); var hLink = ((FieldUrlValue)(oListItem["CISite"])).Url; ClientContext Context = new ClientContext(hLink); Context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; Context.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(Configuration["SharePointUserName"], Configuration["SharePointPassword"]); Context.ExecuteQuery(); var oListData = Context.Web.Lists.GetByTitle("Billeder"); camlQuery.ViewXml = "<View Scope='RecursiveAll'><Query></Query></View>"; ListItemCollection oListDataItem = oListData.GetItems(camlQuery); Context.Load(oListDataItem); Context.ExecuteQuery(); log.Info($"Pictures/Billeder List data pulled successfully. Partner Id {oListItem.GetId()}"); foreach (ListItem oItem in oListDataItem) { try { if (oItem.FileSystemObjectType == FileSystemObjectType.Folder) { Microsoft.SharePoint.Client.Folder ifolder = oItem.Folder; Context.Load(ifolder); Context.ExecuteQuery(); if (!string.IsNullOrWhiteSpace(ifolder.Name.ToLower())) { var pictureFolder = DKBSDbContext.PictureFolder.FirstOrDefault(p => p.FolderName.ToLower() == ifolder.Name.ToLower()); if (pictureFolder == null) { pictureFolder = new PictureFolder { CreatedBy = oItem.GetAuthor(), CreatedDate = oItem.GetCreateDate(), LastModifiedBy = oItem.GetEditor(), LastModified = oItem.GetModifiedDate(), FolderName = ifolder.Name }; DKBSDbContext.PictureFolder.Add(pictureFolder); DKBSDbContext.SaveChanges(); } } } if (oItem.FileSystemObjectType == FileSystemObjectType.File) { PictureFolder pictureFolder = null; Microsoft.SharePoint.Client.File lfile = oItem.File; Context.Load(lfile); Context.ExecuteQuery(); try { //get the folder var tempArray = lfile.ServerRelativeUrl.Replace(@"/" + lfile.Name, "").Split('/'); var folderName = tempArray.ToList().Last(); pictureFolder = DKBSDbContext.PictureFolder.FirstOrDefault(p => p.FolderName.ToLower() == folderName.ToLower()); if (pictureFolder == null) { log.Error($"Unable to find folder for picture. Id:{oItem["ID"]} Picture Name :{lfile.Name} Path : {lfile.ServerRelativeUrl}"); throw new ArgumentNullException($"Unable to find folder for picture. Id:{oItem.GetId()} Picture Name :{lfile.Name} Path : {lfile.ServerRelativeUrl}"); } log.Info($"Picture Name :{lfile.Name} upload started."); var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(Context, lfile.ServerRelativeUrl); await _storageService.SavePictureAsync(fileInfo.Stream, lfile.Name, crmPartner.CRMPartnerId, pictureFolder.PictureFolderId); } catch (Exception ex) { log.Error($"Unable to upload picture. Id:{oItem.GetId()} Picture Name :{lfile.Name}", ex); throw ex; } var picture = new Picture() { CreatedBy = oItem.GetAuthor(), CreatedDate = oItem.GetCreateDate(), LastModifiedBy = oItem.GetEditor(), LastModified = oItem.GetModifiedDate(), CRMPartnerId = crmPartner.CRMPartnerId, PictureFolderId = pictureFolder.PictureFolderId, PictureName = lfile.Name, Title = lfile.Title }; picture.Description = oItem["Description"].ToSharepointString(); picture.Position = oItem["ImagePosition"].ToInt(); DKBSDbContext.Picture.Add(picture); DKBSDbContext.SaveChanges(); log.Info($"Picture Name :{lfile.Name} uploaded successfully."); } } catch (Exception ex) { ErrorCount += 1; log.Error($"Unable to upload picture. Id:{oItem.GetId()}", ex); } } } } catch (Exception ex) { ErrorCount += 1; log.Error($"Partner pictures unable to load Id:{oListItem.GetId()}", ex); } } }