TFS Extension - VS: Add Work Item to Pending Changes via API

I am currently working on a VS extension / add-in, and I need to associate work items with pending changes (without starting registration).

After hours of searching, I could not find a way to accomplish this through the API. The only way I found to associate a work item with pending changes is through Workspace.CheckIn (), which would also trigger registration.

Did I miss something? Or is it really impossible?

0
source share
2 answers

I gave an example code below. Please note: I have not tried this code, but it seems that this is possible only through some reflection, since the API does not have a public method to achieve this. With the new Team Explorer window in VS2012 / 13, you should ideally extend Team Explorer to provide the functionality you intend to use. There are several examples of extending it to MSDN.

Code below, get a copy of the service provider. You can get the IServiceProvider object through a package instance. Once you get it, you need to call the AddWorkItemById method, which is private. Check here for the method definition here .

 int id = workItemId; IPendingChangesExt service = serviceProvider.GetService<IPendingChangesExt>(); FieldInfo field = service.GetType().GetField("m_workItemsSection", BindingFlags.Instance | BindingFlags.NonPublic); Type fieldType = field.FieldType; object value = field.GetValue(service); MethodInfo method = fieldType.GetMethod("AddWorkItemById", BindingFlags.Instance | BindingFlags.NonPublic); object[] objArray = new object[] { id }; method.Invoke(value, objArray); 
+2
source

Sorry for my late reply and many thanks. I had to modify it a bit to make it work:

 // VersionControlExt is needed var dte = Package.GetGlobalService(typeof(DTE)) as DTE; var dte2 = (DTE2)dte; var vce = dte2.DTE.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt") as VersionControlExt; var pendingChangesExtField = vce.PendingChanges.GetType().GetField("m_pendingChangesExt", BindingFlags.Instance | BindingFlags.NonPublic); var pendingChangesExt = pendingChangesExtField.GetValue(vce.PendingChanges); // pendingChangesExt is null when the Pending Changes Window isn't opened if (pendingChangesExt == null) return; var workItemSectionField = pendingChangesExt.GetType().GetField("m_workItemsSection", BindingFlags.Instance | BindingFlags.NonPublic); var workItemSection = workItemSectionField.GetValue(pendingChangesExt); // Assign new Work Item to Pending Changes var addMethod = workItemSectionField.FieldType.GetMethod("AddWorkItemById", BindingFlags.Instance | BindingFlags.NonPublic); object[] addArray = { id }; addMethod.Invoke(workItemSection, addArray); 

And if someone is interested in how to implement drag and drop for the Linked work items section of a pending change window:

 versionControlServer = ServiceProvider.GetService<ITeamFoundationContextManager>().TeamProjectCollection.GetService<VersionControlServer>(); var selectedItems = new[] {1, 2}; var dropData = new WorkItemDropData(versionControlServer.ServerGuid, selectedItems); var dataObject = new DataObject("Microsoft.TeamFoundation.WorkItemId", dropData); DragDrop.DoDragDrop(listView, dataObject, DragDropEffects.Move); 
+2
source

Source: https://habr.com/ru/post/1244682/


All Articles