public void GetWorkItem()
        {
            var wi = WIStore.GetWorkItem(WorkItemsAdded.First());

            Console.WriteLine($"Opened a work item with id: '{wi.Id}' and title: '{wi.Title}'");
            Console.WriteLine();
        }
        public void GetWorkItems()
        {
            WorkItem wi = new WorkItem(DefaultWorkItemType)
            {
                Title = "Work Item Created Using WIT OM"
            };

            wi.Save();
            WorkItemsAdded.Add(wi.Id);
            wi.Close();

            var workItemsList = string.Join(",", WorkItemsAdded);
            // Can get multiple work items by using a wiql query
            string             wiql             = $"Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.Id] In ({workItemsList})";
            WorkItemCollection wiqlQueryResults = WIStore.Query(wiql);

            Console.WriteLine($"Wiql query searching for work items in ({workItemsList}) returned {wiqlQueryResults.Count} values");
            Console.WriteLine();

            // Alternatively, you can specify the ids as a collection, and a wiql for columns to return
            string             columnWiql             = $"Select [System.Id], [System.Title], [System.State] From WorkItems ";
            WorkItemCollection idAndColumnWiqlResults = WIStore.Query(WorkItemsAdded.ToArray(), columnWiql);

            Console.WriteLine($"Query searching for work items in ({workItemsList}) returned {idAndColumnWiqlResults.Count} values");
            Console.WriteLine();
        }
        public void LinkExistingWorkItem()
        {
            // Get the first item
            var firstWorkItem = WIStore.GetWorkItem(WorkItemsAdded.First());

            // Create a new work item
            var secondWorkItem = new WorkItem(DefaultWorkItemType)
            {
                Title = "Second work item created"
            };

            secondWorkItem.Save();

            // Need to know the type of link type ends available to the project
            var linkTypeEnds = WIStore.WorkItemLinkTypes.LinkTypeEnds;

            // Create a new work item type link with the specified type and the work item to point to
            // Access WorkItemLinkTypeEnd by specifying ImmutableName as index
            var relatedLinkTypeEnd = linkTypeEnds["System.LinkTypes.Related-Forward"];
            var workItemLink       = new WorkItemLink(relatedLinkTypeEnd, secondWorkItem.Id);

            // Add the work item link to the desired work item and save
            firstWorkItem.WorkItemLinks.Add(workItemLink);
            firstWorkItem.Save();

            Console.WriteLine($"Added a link from existing work item '{secondWorkItem.Id}' to '{firstWorkItem.Id}.' Work Item: '{firstWorkItem.Id}' contains a link to '{secondWorkItem.Id}': {firstWorkItem.Links.Contains(workItemLink)}");
            firstWorkItem.Links.Contains(workItemLink);
            Console.WriteLine();
        }
        public void QueryByWiql()
        {
            string             queryByTypeWiql  = $"Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = '{DefaultWorkItemType.Name}' and [System.TeamProject] = '{TeamProject.Name}'";
            WorkItemCollection typeQueryResults = WIStore.Query(queryByTypeWiql);

            Console.WriteLine($"The wiql query returned {typeQueryResults.Count} results:");
            foreach (WorkItem result in typeQueryResults)
            {
                Console.WriteLine($"WorkItem Id: '{result.Id}' Title: '{result.Title}'");
            }
            Console.WriteLine();
        }
        public void AddHyperLink()
        {
            var       wi    = WIStore.GetWorkItem(WorkItemsAdded.First());
            Hyperlink hlink = new Hyperlink("https://www.microsoft.com")
            {
                Comment = "This is a hyperlink to microsoft.com"
            };

            wi.Links.Add(hlink);
            wi.Save();
            Console.WriteLine($"Updated Existing Work Item: '{wi.Id}'. Added hyperlink: '{hlink.Location}'");
            Console.WriteLine();
        }
        public void QueryById()
        {
            // Querying by ID doesn't really exist in the WIT model like it does in REST.
            // In this case, we are looking up the query definition by its ID and then creating a new Query with its corresponding wiql

            // Get an existing query and associated ID for proof of concept. If we already have a query guid, we can skip this block.
            QueryHierarchy queryHierarchy  = TeamProject.QueryHierarchy;
            var            queryFolder     = queryHierarchy as QueryFolder;
            QueryItem      queryItem       = queryFolder?.Where(f => f.IsPersonal).FirstOrDefault();
            QueryFolder    myQueriesFolder = queryItem as QueryFolder;

            if (myQueriesFolder != null && myQueriesFolder.Count > 0)
            {
                var queryId = myQueriesFolder.First().Id; // Replace this value with your query id

                // Get the query definition
                var queryDefinitionById = WIStore.GetQueryDefinition(queryId);
                var context             = new Dictionary <string, string>()
                {
                    { "project", TeamProject.Name }
                };

                // Obtain query results using the query definition's QueryText
                Query obj = new Query(this.WIStore, queryDefinitionById.QueryText, context);
                WorkItemCollection queryResults = obj.RunQuery();
                Console.WriteLine($"Query with name: '{queryDefinitionById.Name}' and id: '{queryDefinitionById.Id}' returned {queryResults.Count} results:");
                if (queryResults.Count > 0)
                {
                    foreach (WorkItem result in queryResults)
                    {
                        Console.WriteLine($"WorkItem Id: '{result.Id}' Title: '{result.Title}'");
                    }
                }
                else
                {
                    Console.WriteLine($"Query with name: '{queryDefinitionById.Name}' and id: '{queryDefinitionById.Id}' did not return any results.");
                    Console.WriteLine($"Try assigning work items to yourself or following work items and run the sample again.");
                }
            }

            else
            {
                Console.WriteLine("My Queries haven't been populated yet. Open up the Queries page in the browser to populate these, and then run the sample again.");
            }

            Console.WriteLine();
        }
        public void UpdateExistingWorkItem()
        {
            var wi                  = WIStore.GetWorkItem(WorkItemsAdded.First());
            var originalTitle       = wi.Title;
            var originalDescription = wi["System.Description"];

            var changedTitle       = "Changed Work Item Title";
            var changedDescription = "Changed Description";

            wi.Title = changedTitle;
            wi["System.Description"] = changedDescription;

            wi.Save();
            Console.WriteLine($"Updated Existing Work Item: '{wi.Id}'. Work Item title was: '{originalTitle}', but is now '{wi.Title}'");
            Console.WriteLine($"Updated Existing Work Item: '{wi.Id}'. Work Item description was: '{originalDescription}', but is now '{wi["System.Description"] }'");
            Console.WriteLine();
        }
        public void AddComment()
        {
            var wi = WIStore.GetWorkItem(WorkItemsAdded.First());

            // The value for field 'System.History' at the current revision is always an empty string.
            var currentHistoryValue = wi.History;

            Console.WriteLine($"WorkItem History is empty at the current revision: '{String.IsNullOrEmpty(currentHistoryValue)}'");

            var commentToAdd = "Added a new comment";

            wi.History = commentToAdd;
            wi.Save();

            // In the WIT model, when the history field is saved, the recent change is persisted in the previous revision and current revision becomes empty.
            Console.WriteLine($"Updated Existing Work Item: '{wi.Id}'. Added comment: '{wi.Revisions[wi.Revisions.Count - 1].Fields[CoreField.History].Value}' to last revision");
            Console.WriteLine();
        }
        public void AddAttachment()
        {
            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");
                }
            }

            var        wi            = WIStore.GetWorkItem(WorkItemsAdded.First());
            Attachment newAttachment = new Attachment(filePath);

            wi.Attachments.Add(newAttachment);
            wi.Save();
            Console.WriteLine($"Updated Existing Work Item: '{wi.Id}'. Added attachment: '{newAttachment.Name}'");
            Console.WriteLine();
        }