TFS - VS Extension: Add work item to pending changes via API
Asked Answered
N

2

0

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

After searching for some hours I couldn't find a way to accomplish this via the API. The only way I found to associate a work item to the pending changes is Workspace.CheckIn() which also would trigger the check-in..

Do I miss something? Or is this really not possible?

Nonillion answered 15/8, 2014 at 18:16 Comment(1)
Can you even do that via GUI?Kasi
M
2

I have provided the sample code below, Note that, I have not tried this code, but it seems it is only possible through some reflection as there is no public method to achieve this in the API. With the new Team Explorer window in VS2012/13, you would ideally extend the Team Explorer to provide the functionality you intend to. There are some examples of extending it on MSDN.

The code below, get the service provider instance. You can get the IServiceProvider object, through your package instance. Once you get it, you need to call AddWorkItemById method which is private - Check 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);
Montiel answered 17/8, 2014 at 18:48 Comment(0)
N
2

Sorry for my late reply and thank you very much. I had to modify it a little to get it working:

// 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 how to implement drag & drop for the Related Work Items section of the Pending Changes 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);
Nonillion answered 21/11, 2014 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.