How to Associate an work item as "Associate" or "Resolve" in Visual Studio TFS API (VS2015)?

Eduardo Elias Saléh picture Eduardo Elias Saléh · Mar 8, 2016 · Viewed 13.5k times · Source

I have forked a project with the code below:

var pc = ParentSection.GetService<IPendingChangesExt>();

var model = pc.GetType().GetField("m_workItemsSection", BindingFlags.NonPublic | BindingFlags.Instance);
var t = model.FieldType;
var mm = model.GetValue(pc);

var m = t.GetMethod("AddWorkItemById", BindingFlags.NonPublic | BindingFlags.Instance);

m.Invoke(mm, new object[] { selectedWorkItemId });

That adds an work item by its ID to the current pending changes.

Now, I want to link the work items choosing between "Associate" or "Resolve" (associate and resolve), depending on which button the user clicked on plugin's interface as below: enter image description here

If the user clicks on 'Associate and resolve', the work item should be associated and marked as resolved upon check-in.

If the user clicks on 'Associate only' the work item must be only linked to changeset, but not resolved.

Any help will be welcome

Answer

PatrickLu-MSFT picture PatrickLu-MSFT · Mar 9, 2016

This is not a property of WorkItem. This is the Check-in action on the work item. Details you can refer this link Changing the Default CheckIn Option to Associate in TFS 2012

You may need to use CheckinWorkItemAction Enumeration. More detail info from MSDN.

Similar question about TFS - VS Extension: Add work item to pending changes via API and also check this link: C# Programmatically Checking in code changes with TFS API while associating the changeset to a Work Item


Update

If you want to change the default state 'resolve' to 'associate', you need to

  • change the set the registry key HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\1x.0\TeamFoundation\SourceControl\Behavior\ResolveAsDefaultCheckinAction to False.

  • Or For VS2015, there is an option Tools > Options > Source Control > Visual Studio Team Foundation > "Resolve associated work items on check-in".

enter image description here

Note: Both of above only affects your client machine.

Otherwise, for all users, you need to edit the Work Item Template definition for the types of work items you are using (Bug, Task, etc.). Details steps you can refer this question How to disable auto done status for task in checkin