public void GetWorkItem() { WorkItem wi = WitClient.GetWorkItemAsync(WorkItemsAdded.First()).Result; Console.WriteLine($"Opened a work item with id: '{wi.Id}' and title: '{wi.Fields["System.Title"]}'"); Console.WriteLine(); }
public void AddHyperLink() { string hyperlinkToAdd = "https://www.microsoft.com"; JsonPatchDocument patchDocument = new JsonPatchDocument { new JsonPatchOperation() { Operation = Operation.Add, Path = "/relations/-", Value = new { rel = "HyperLink", url = hyperlinkToAdd, attributes = new { comment = "Microsoft" } } } }; WorkItem wi = WitClient.GetWorkItemAsync(this.WorkItemsAdded.First()).Result; var relations = wi.Relations?.Where(r => r.Rel == "Hyperlink") ?? new List <WorkItemRelation>(); var previousRelationsCount = relations.Count(); var result = WitClient.UpdateWorkItemAsync(patchDocument, wi.Id.Value).Result; var newHyperlinks = result.Relations?.Where(r => r.Rel == "Hyperlink"); var newHyperlinksCount = newHyperlinks.Count(); Console.WriteLine($"Updated Existing Work Item: '{wi.Id}'. Had {previousRelationsCount} hyperlinks, now has {newHyperlinksCount}"); Console.WriteLine(); }
public void AddComment() { // Get a work item WorkItem wi = WitClient.GetWorkItemAsync(this.WorkItemsAdded.First()).Result; // Get the current last comment of the work item WorkItemComments comments = WitClient.GetCommentsAsync(wi.Id.Value).Result; var originalCommentCount = comments.Count; // Create a JSON patch document with an entry updating System.History JsonPatchDocument patchDocument = new JsonPatchDocument { new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.History", Value = "Added a comment" } }; // Update the work item with the patch document var result = WitClient.UpdateWorkItemAsync(patchDocument, wi.Id.Value).Result; // Get the current last comment of the work item var updatedComments = WitClient.GetCommentsAsync(result.Id.Value).Result; var updatedCommentCount = updatedComments.Count; // Show that the current last comment is different than the original last comment Console.WriteLine($"There were {originalCommentCount} comments"); Console.WriteLine($"There are now {updatedCommentCount} comments"); Console.WriteLine(); }
public Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem GetWorkItem(int Id) { try { return(WitClient.GetWorkItemAsync(Id).Result); } catch { return(null); } }
public void AddAttachment() { // Create a file to attach with sample text var filePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); using (FileStream fstream = File.Create(filePath)) { using (StreamWriter swriter = new StreamWriter(fstream)) { swriter.Write("Sample attachment text"); } } // Upload attachment AttachmentReference attachment = WitClient.CreateAttachmentAsync(filePath).Result; Console.WriteLine("Attachment created"); Console.WriteLine($"ID: {attachment.Id}"); Console.WriteLine($"URL: '{attachment.Url}'"); Console.WriteLine(); // Get an existing work item and add the attachment to it WorkItem wi = WitClient.GetWorkItemAsync(this.WorkItemsAdded.First()).Result; JsonPatchDocument attachmentPatchDocument = new JsonPatchDocument { new JsonPatchOperation() { Operation = Operation.Add, Path = "/relations/-", Value = new { rel = "AttachedFile", url = attachment.Url, attributes = new { comment = "Attached a file" } } } }; var attachments = wi.Relations?.Where(r => r.Rel == "AttachedFile") ?? new List <WorkItemRelation>(); var previousAttachmentsCount = attachments.Count(); var result = WitClient.UpdateWorkItemAsync(attachmentPatchDocument, wi.Id.Value).Result; var newAttachments = result.Relations?.Where(r => r.Rel == "AttachedFile"); var newAttachmentsCount = newAttachments.Count(); Console.WriteLine($"Updated Existing Work Item: '{wi.Id}'. Had {previousAttachmentsCount} attachments, now has {newAttachmentsCount}"); Console.WriteLine(); }
public WorkItem GetWorkItem(string pUrl) { int _id = GetWIIDFromUrl(pUrl); if (_id > 0) { return(WitClient.GetWorkItemAsync(_id, expand: WorkItemExpand.Relations).Result); } else { return(null); } }
public void ValidateWorkItem() { try { // Create new work item var createPatchDocument = new JsonPatchDocument { new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.History", Value = "Modify system history" } }; // Set validateOnly param to true and attempt to create a work item with an incomplete patch document (missing required title field). var validateOnCreateWI = WitClient.CreateWorkItemAsync(createPatchDocument, TeamProject.Name, DefaultWorkItemType.Name, true).Result; } catch (AggregateException ex) { Console.WriteLine(ex.InnerException.Message); Console.WriteLine(); } // Update existing work item try { var wi = WitClient.GetWorkItemAsync(WorkItemsAdded.First()).Result; var updatePatchDocument = new JsonPatchDocument { new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.AreaPath", Value = "Invalid area path" } }; // Set validateOnly param to true and attempt to update a work item with an invalid field entry. var validateOnUpdateWI = WitClient.UpdateWorkItemAsync(updatePatchDocument, wi.Id.Value, true).Result; } catch (AggregateException ex) { Console.WriteLine(ex.InnerException.Message); Console.WriteLine(); } Console.WriteLine(); }
public void UpdateExistingWorkItem() { var wi = WitClient.GetWorkItemAsync(WorkItemsAdded.First()).Result; var originalTitle = wi.Fields["System.Title"]; JsonPatchDocument patchDocument = new JsonPatchDocument { new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.Title", Value = "Changed Work Item Title Using REST Client" } }; var result = WitClient.UpdateWorkItemAsync(patchDocument, wi.Id.Value).Result; Console.WriteLine($"Workitem: '{wi.Id}' title updated from: '{originalTitle}' to: '{result.Fields["System.Title"]}'"); Console.WriteLine(); }
public void LinkExistingWorkItem() { // Get existing work item to link to new work item. WorkItem existingWI = WitClient.GetWorkItemAsync(WorkItemsAdded.First()).Result; // Create a patch document for a new work item. // Specify a relation to the existing work item. JsonPatchDocument patchDocument = new JsonPatchDocument { new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.Title", Value = "New work item to link to" }, new JsonPatchOperation() { Operation = Operation.Add, Path = "/relations/-", Value = new { rel = "System.LinkTypes.Hierarchy-Reverse", url = existingWI.Url, attributes = new { comment = "adding a link to an existing work item" } } } }; // Create a new work item and link it to the existing work item (using patchdocument) WorkItem newWI = WitClient.CreateWorkItemAsync(patchDocument, TeamProject.Id, DefaultWorkItemType.Name).Result; Console.WriteLine($"Created a new work item Id:{newWI.Id}, Title:{newWI.Fields["System.Title"]}"); foreach (var relation in newWI.Relations) { Console.WriteLine($"{relation.Rel} {relation.Title} {relation.Url}"); } Console.WriteLine(); }
public WorkItem GetWorkItem(int pId) { return(WitClient.GetWorkItemAsync(pId, expand: WorkItemExpand.All).Result); }