public void DownloadAttachment(System.Guid id, string saveToFile) { VssConnection connection = new VssConnection(_uri, _credentials); WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient <WorkItemTrackingHttpClient>(); Stream attachmentStream = workItemTrackingHttpClient.GetAttachmentContentAsync(id).Result; int length = 256; int bytesRead; Byte[] buffer = new Byte[length]; FileStream writeStream = new FileStream(@saveToFile, FileMode.Create, FileAccess.ReadWrite); bytesRead = attachmentStream.Read(buffer, 0, length); // read data write stream while (bytesRead > 0) { writeStream.Write(buffer, 0, bytesRead); bytesRead = attachmentStream.Read(buffer, 0, length); } attachmentStream.Close(); writeStream.Close(); }
public async static Task <Stream> GetAttachmentAsync(WorkItemTrackingHttpClient client, Guid id) { return(await RetryHelper.RetryAsync(async() => { return await client.GetAttachmentContentAsync(id); }, 5)); }
public bool DownloadAttachment() { Guid attachmentId; if (!Context.TryGetValue <Guid>("$attachmentId", out attachmentId)) { throw new Exception("Run the upload attachent sample prior to running this sample."); } string fileFullPath = Path.GetTempFileName(); // Get the client VssConnection connection = Context.Connection; WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient <WorkItemTrackingHttpClient>(); // Get a stream for the attachment Stream attachmentStream = workItemTrackingClient.GetAttachmentContentAsync(attachmentId).Result; // Write the file to disk using (FileStream writeStream = new FileStream(fileFullPath, FileMode.Create, FileAccess.ReadWrite)) { attachmentStream.CopyTo(writeStream); } Console.WriteLine("Attachment downloaded"); Console.WriteLine(" Full path: {0}", fileFullPath); return(true); }
public void GetAllAttachmentsOnWorkItem() { //this assumes you created a work item first from the WorkItemsSample.cs class //if not, you can manually add a work item id int workitemId = 0; string fileFullPath = Path.GetTempFileName(); //check to see if the work item id is set in cache try { workitemId = Convert.ToInt32(Context.GetValue <WorkItem>("$newWorkItem1").Id); } catch (Exception) { Console.WriteLine("No work item found in cache. Either create a new work item and attachments or manually set the id for known work item id."); return; } VssConnection connection = Context.Connection; WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient <WorkItemTrackingHttpClient>(); WorkItem workitem = workItemTrackingClient.GetWorkItemAsync(workitemId, null, null, WorkItemExpand.Relations, null).Result; if (workitem == null) { Console.WriteLine("No work item found for id"); return; } Console.WriteLine("Getting attachments on work item"); if (workitem.Relations == null || workitem.Relations.Count == 0) { Console.WriteLine("No attachments found on work item"); return; } foreach (var item in workitem.Relations) { if (item.Rel == "AttachedFile") { //string manipulation to get the guid off the end of the url string[] splitString = item.Url.ToString().Split('/'); Guid attachmentId = new Guid(splitString[7].ToString()); Console.WriteLine("Getting attachment name and id: {0}", item.Attributes.GetValueOrDefault("name").ToString()); Stream attachmentStream = workItemTrackingClient.GetAttachmentContentAsync(attachmentId).Result; using (FileStream writeStream = new FileStream(fileFullPath, FileMode.Create, FileAccess.ReadWrite)) { attachmentStream.CopyTo(writeStream); } } } return; }
private static void RunOptionsAndReturnExitCode(Options opts) { Console.WriteLine($"Authorizing to VSTS..."); try { var collectionUrl = opts.CollectionUrl.ToLower().StartsWith("https://") ? opts.CollectionUrl : "https://" + opts.CollectionUrl; VssConnection connection = new VssConnection(new Uri(collectionUrl), new VssClientCredentials(false)); WorkItemTrackingHttpClient witClient = connection.GetClient <WorkItemTrackingHttpClient>(); WorkItemQueryResult queryResults = witClient.QueryByIdAsync(opts.QueryId).Result; if (queryResults == null) { Console.WriteLine("Query result is null."); return; } var idList = new List <int>(); if (queryResults.QueryType == QueryType.Flat) { if (queryResults.WorkItems == null || queryResults.WorkItems.Count() == 0) { Console.WriteLine("Query did not find any results"); return; } foreach (var item in queryResults.WorkItems) { idList.Add(item.Id); } } else { if (queryResults.WorkItemRelations == null || queryResults.WorkItemRelations.Count() == 0) { Console.WriteLine("Query did not find any results"); return; } foreach (var item in queryResults.WorkItemRelations) { idList.Add(item.Target.Id); } } foreach (int id in idList) { var attachmentString = opts.Latest ? "the latest attachment" : "all attachments"; Console.Write($"Downloading {attachmentString} for {id}... "); var workitem = witClient.GetWorkItemAsync(id, null, null, WorkItemExpand.Relations, null).Result; if (workitem.Relations == null || workitem.Relations.Count == 0 || workitem.Relations.All(r => r.Rel != "AttachedFile")) { Console.WriteLine("No attachment"); continue; } foreach (var relation in workitem.Relations .Where(r => { if (r.Rel == "AttachedFile") { return(string.IsNullOrEmpty(opts.Keyword) ? true : r.Attributes.GetValueOrDefault("name").ToString().ToLower().Contains(opts.Keyword.ToLower())); } return(false); }) .OrderByDescending(r => (DateTime)r.Attributes.GetValueOrDefault("authorizedDate"))) { //Get the guid from the end of the url string attachmentId = relation.Url.ToString().Split('/').Last(); var filename = relation.Attributes.GetValueOrDefault("name").ToString(); Stream attachmentStream = witClient.GetAttachmentContentAsync(new Guid(attachmentId)).Result; var fileMode = opts.Overwrite ? FileMode.Create : FileMode.Create; using (FileStream writeStream = new FileStream(CreateOutputFilePath(opts, id, filename), fileMode, FileAccess.ReadWrite)) { attachmentStream.CopyTo(writeStream); } if (opts.Latest) { break; } } Console.WriteLine("Done"); } Console.WriteLine("All Done!"); } catch (Exception ex) { Exception innerEx = ex; while (innerEx.InnerException != null) { innerEx = innerEx.InnerException; } Console.WriteLine(innerEx.Message); } }