public ActionResult GetWorkItems(string accountName, string projectName) { AccountDetail accountDetail = new AccountDetail { WorkItemDetails = new List <WorkItemDetail>(), SelectedAccountName = accountName, SelectedProjectName = projectName }; try { if (Convert.ToString(Session["PAT"]) != null) { string token = Convert.ToString(Session["PAT"]); CLWorkItem cLWorkItem = new CLWorkItem(token); var workItems = cLWorkItem.GetWorkItemsByQuery(accountName, projectName); if (workItems.Status) { var splitWI = cLWorkItem.WIAsList(workItems.ResponseAsDynamicObj.workItems); foreach (var wi in splitWI) { var wiDetils = cLWorkItem.GetWIByIds(accountName, projectName, wi); if (wiDetils.Status) { var wiDs = JsonConvert.DeserializeObject <AzureDevOpsService.Models.WorkItemFetchResponse.WorkItems>(wiDetils.ResponseAsString); foreach (var widetail in wiDs.value) { var wiDetailsMdel = new WorkItemDetail { Id = Convert.ToString(widetail.id), Name = Convert.ToString(widetail.fields.SystemTitle), Type = Convert.ToString(widetail.fields.SystemWorkItemType), AttachmentPath = new List <Attachment>() }; if (widetail.relations != null) { foreach (var rel in widetail.relations) { if (rel != null) { if (rel.rel == "AttachedFile") { Attachment attachment = new Attachment { Uri = rel.url }; string attachmentUrl = rel.url; int index = attachmentUrl.LastIndexOf("/"); string attachmentId = attachmentUrl.Substring(index + 1); attachment.AttachmentId = attachmentId; attachment.Name = rel.attributes["name"]; wiDetailsMdel.AttachmentPath.Add(attachment); } } //wiDetailsMdel.AttachmentPath.Add() } } accountDetail.WorkItemDetails.Add(wiDetailsMdel); } } } } return(PartialView("_GetWorkItems", accountDetail)); } else { return(RedirectToAction("../Account/Verify")); } } catch (Exception) { return(View(accountDetail)); } }
public ActionResult DownloadAttachments(string data) { Download model = JsonConvert.DeserializeObject <Download>(data); CreateZip.DirectoriesFiles sfiles = new CreateZip.DirectoriesFiles { Files = new List <CreateZip.FileInfo>(), Folder = new List <CreateZip.Folder>() }; try { if (Convert.ToString(Session["PAT"]) != null) { string token = Convert.ToString(Session["PAT"]); CLWorkItem cLWorkItem = new CLWorkItem(token); // the output bytes of the zip byte[] fileBytes = null; if (model.ExportType == "File") { foreach (var wi in model.DocumentIds) { CreateZip.FileInfo fileInfo = new CreateZip.FileInfo(); fileInfo.FileBytes = cLWorkItem.DownloadAttachment(model.AccountName, model.ProjectName, wi.DocId, wi.DocName); String docName = wi.DocName; int index = docName.LastIndexOf("."); String documentName = docName.Substring(0, index); String documentExtension = docName.Substring(index + 1); fileInfo.Name = wi.WorkItemId + "__" + documentName; fileInfo.Extension = documentExtension; sfiles.Files.Add(fileInfo); } //create a working memory stream using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { // create a zip using (System.IO.Compression.ZipArchive zip = new System.IO.Compression.ZipArchive(memoryStream, System.IO.Compression.ZipArchiveMode.Create, true)) { if (sfiles.Files != null && sfiles.Files.Count > 0) { foreach (var outerFile in sfiles.Files) { // add the item name to the zip System.IO.Compression.ZipArchiveEntry zipItem = zip.CreateEntry(outerFile.Name + "." + outerFile.Extension); // add the item bytes to the zip entry by opening the original file and copying the bytes using (System.IO.MemoryStream originalFileMemoryStream = new System.IO.MemoryStream(outerFile.FileBytes)) { using (System.IO.Stream entryStream = zipItem.Open()) { originalFileMemoryStream.CopyTo(entryStream); } } } } } fileBytes = memoryStream.ToArray(); } } else { CreateZip.Folder folder = new CreateZip.Folder(); foreach (var wi in model.DocumentIds) { CreateZip.Folder folderq = new CreateZip.Folder(); folderq.FolderItems = new List <CreateZip.FolderItem>(); CreateZip.FolderItem folderItem = new CreateZip.FolderItem(); folderq.FolderName = wi.WorkItemId; String fDocName = wi.DocName; int fIndex = fDocName.LastIndexOf("."); String folderItemName = fDocName.Substring(0, fIndex); String folderItemExtension = fDocName.Substring(fIndex + 1); folderItem.Name = folderItemName; folderItem.Extension = folderItemExtension; folderItem.FileBytes = cLWorkItem.DownloadAttachment(model.AccountName, model.ProjectName, wi.DocId, wi.DocName); folderq.FolderItems.Add(folderItem); sfiles.Folder.Add(folderq); } using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { // create a zip using (System.IO.Compression.ZipArchive zip = new System.IO.Compression.ZipArchive(memoryStream, System.IO.Compression.ZipArchiveMode.Create, true)) { if (sfiles.Folder != null && sfiles.Folder.Count > 0) { foreach (var fldr in sfiles.Folder) { // add the item name to the zip // each file in the folder foreach (var file in fldr.FolderItems) { // add the item name to the zip System.IO.Compression.ZipArchiveEntry zipItem = zip.CreateEntry(fldr.FolderName + "/" + file.Name + "." + file.Extension); // add the item bytes to the zip entry by opening the original file and copying the bytes using (System.IO.MemoryStream originalFileMemoryStream = new System.IO.MemoryStream(file.FileBytes)) { using (System.IO.Stream entryStream = zipItem.Open()) { originalFileMemoryStream.CopyTo(entryStream); } } } } } } fileBytes = memoryStream.ToArray(); } } // download the constructed zip Response.AddHeader("Content-Disposition", "attachment; filename=" + "WIAttachments_" + model.ProjectName + ".zip"); return(File(fileBytes, "application/zip")); } else { return(RedirectToAction("../Account/Verify")); } } catch (Exception ex) { logger.Append(ex.Message); logger.Append(ex.StackTrace); return(RedirectToAction("../Account/Verify")); } }