query regarding TFS workitem - tfs

I want to know how I can update the field of my bug workitem. Suppose I need to change title of my bug workitem and after doing that I should get one pop up message box that my title field has changed without using email alerts? This is the query to select the workitem of a particular team project:
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection
(new Uri("server url"));
var service = tfs.GetService<WorkItemStore>();
var qText = String.Format(#"SELECT [System.WorkItemType],
[System.Title], [System.Description], [System.Reason]
FROM WorkItems WHERE [System.TeamProject] = {0}", "'Demo1'");
I want to know the update query for changing the particular field.

Have you tried something like:
Dim workItemStore as WorkItemStore = tfs.GetService(Of WorkItemStore)()
Dim wi as WorkItem = workItemStore.GetWorkItem(workItemNumber)
wi.Fields("System.Title").Value = "Foo Title"
wi.Save()

I don't think WIQL supports dml commands. You are probably going to have to use the object model to do this: http://msdn.microsoft.com/en-us/library/bb130323.aspx

Related

Team Foundation Server - Backdate Epics

I am taking over a catalog of projects that already initiated. My job is to enter the projects into TFS and use the 'work' features. I am entering each project as an Epic...15 Epics total. When I enter an Epic is used the date I enter it as the project initiation day. Is there a way to grandfather in Epics in TFS. Adding Epics with a start date in the past? I cannot congigure the cumulative flow diagram properly without being able to back date entries.
Thanks
In Epics, the default field "CreateDate" can't be changed. It uses the date and time when the Epic created.
As a alternative, you can customize a field to use DateTimeControl, then you can select Date and change the time in this custom field:
There isn't any way to change the create date from TFS Web Portal. But if you create the work item via TFS API, you can enable the bypassrule which allow you specify the create date of the work item. Following is a code sample for this:
static void Main(string[] args)
{
string url = "http://collectionurl/";
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(url));
WorkItemStore wis = new WorkItemStore(ttpc.Name,WorkItemStoreFlags.BypassRules);
Project pro = wis.Projects["ProjectName"];
WorkItemTypeCollection wits = pro.WorkItemTypes;
WorkItem wi = new WorkItem(wits["Epic"]);
wi.Fields["System.Title"].Value = "Title";
wi.Fields["System.CreatedDate"].Value = Convert.ToDateTime("2016-09-01");
wi.Save();
}
Note: To use this, you must be the member of "Project Collection Administrators" security group.
Refer to this link for details: TFS API Part 48 – WorkItemControl And Bypass Work Item Rules.

Tfs Can not Get Project Name from Query programmatically using #Project parameter

i'm looking for your help, guys! I want to get Current Project Name from query, using my custom plugin.
Here is my code
WorkItemCollection queryResults = workItemStore.Query("SELECT [System.TeamProject] FROM WorkItems WHERE [System.TeamProject] = '#Project'");
foreach (WorkItem item in queryResults)
{
// SomeCode;
}
So result of Query is empty.. I have no idea why.. If i'm writing
real Project Name instead of the '#Project' it's works.. Also i tried to write #Project wihout quotes - also no result.
You can't use "#Project" in code like this. #Project is only available within a VisualStudio work item query where it can infer the team project based on your currently selected project in the Team Explorer.
You can try this code if your plugin is an WorkItemChangedEventHandler:
WorkItemChangedEvent workItemChange = (WorkItemChangedEvent)notificationEventArgs;
Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem wi = workItemStore.GetWorkItem(workItemChange.CoreFields.IntegerFields[0].NewValue);
string p = wi.Project.Name

TFS Custom Field Reporting on Historical Value

Using Agile template on with some customized fields.
Custom-Field-X is a Drop Down List with allowed value set 0, 1, 2, 3 - this value could change daily.
Need a way to check if Custom-Field-X was ever set to 0 during its life time.
Using TFS2010.
Thanks!
I assume you are talking about a custom field you added for TFS workitems. You can use following code to find out whether the value was set to 0 or not. Alternately you can also use workItemStore.GetWorkItem(id) to get specific workitem by Id. You can find details about retrieving work items # http://pwee167.wordpress.com/2012/09/18/retrieving-work-items-using-the-team-foundation-server-api/.
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client; // You need to add reference to both these assemblies in your project
var collectionUri = new Uri("<TfsUrl>/<CollectionName>"); // For e.g. "http://tfs:8080/DefaultCollection"
var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(collectionUri);
WorkItemStore workItemStore = projectCollection.GetService<WorkItemStore>();
var results = workItemStore.Query("SELECT * FROM WORKITEMS");
WorkItem workItem = results[0];
foreach (Revision revision in workItem.Revisions)
{
var originalValue = revision.Fields["Custom-Field-X"].OriginalValue;
var curretValue = revision.Fields["Custom-Field-X"].Value;
}

Access the Kanban Column (a Team-Specific Field) for a Work Item

Is there a way to programmatically access the "Kanban Column" for a WorkItem using the TFS 2012 API?
Using the Scrum 2.2 template, the history of a Bug or Product Backlog Item shows "[MyProject\MyTeam] Kanban Column" as a changed field whenever a work item is dragged between Kanban columns on the Board, but the field is not accessible when specifically retrieving a work item through the TFS API.
It also shows up as a changed field in the WorkItemChangedEvent object when implementing the ProcessEvent method on the Microsoft.TeamFoundation.Framework.Server.ISubscriber interface.
Workaround:
A coworker found a blogpost about creating a read-only custom field to persist the value of the Kanban Column, taking advantage of the WorkItemChangedEvent to capture the latest value. It is then possible to query on this column. One problem with this approach is that only a single team's Kanban Column can be tracked.
Update:
According to this blogpost, the Kanban Column is not a field, rather a "WIT Extension". This may help lead to an answer.
I've found a way to read the value using the TFS 2013 API, inside the ISubscriber.ProcessEvent method:
var workItemId = 12345;
var extService = new WorkItemTypeExtensionService();
var workItemService = new WorkItemService();
var wit = workItemService.GetWorkItem(requestContext, workItemId);
foreach (var wef in extService.GetExtensions(requestContext, wit.WorkItemTypeExtensionIds))
{
foreach (var field in wef.Fields)
{
if (field.LocalName == "Kanban Column" || field.LocalName == "Backlog items Column")
{
// Access the new column name
var columnName = wit.LatestData[field.Field.FieldId];
}
}
}
If you are prepared to dig into the database you can mine this information out. I don't fully understand the modelling of the teams in TFS yet but first you need to work out which field id the team of interest is storing the Kanban state in as follows (TFS 2012):
USE Tfs_DefaultCollection
SELECT TOP(10)
MarkerField + 1 as FieldId,*
FROM tbl_WorkItemTypeExtensions with(nolock)
JOIN tbl_projects on tbl_WorkItemTypeExtensions.ProjectId = tbl_projects.project_id
WHERE tbl_projects.project_name LIKE '%ProjectName%
Then replace XXXXXXXX below with the FieldId discovered above
SELECT TOP 1000
wid.Id,
wia.State,
wid.StringValue as Kanban,
wia.[Work Item Type],
wia.Title,
tn.Name as Iteration
FROM tbl_WorkItemData wid with(nolock)
JOIN WorkItemsAre wia on wia.ID = wid.Id
JOIN TreeNodes tn on wia.IterationID = tn.ID
WHERE FieldId = XXXXXXXX and RevisedDate = '9999-01-01 00:00:00.000'
ORDER BY Id
I am not familiar with the Scrum 2.2 template, but the works are the same for CMMI or Scrum templates when it comes to TFS Work Item tracking.
Try something like this:
public string GetKanbanColumn(WorkItem wi)
{
if (wi != null)
{
return wi["Kanban"].ToString();
}
return string.Empty;
}
Depending on the actual name of the column, specified in the Work Item Template XML file. Hope this helps.

What tfs 2010 object references are available for Tfs_DefaultCollection.dbo.tbl_ReleaseNoteDetails

We have a Team Project Collection Source Control Setting for Check-in Notes that requires each check-in to capture a "Tracking Number". This number is external to TFS. I need to search for all the changesets that have a specific Tracking number.
The resulting changeset list tells me what to GetLatest on, for our monthly deployment.
-- We don't use Work Items
This .SQL gives me the list I'm looking for. I want to access this in code from Visual Studio.
SELECT ReleaseNoteId, FieldName, BaseValue
from Tfs_DefaultCollection.dbo.tbl_ReleaseNoteDetails
where ReleaseNoteId in (SELECT ReleaseNoteId
FROM Tfs_DefaultCollection.dbo.tbl_ReleaseNote
where DateCreated between '2013-01-01' and '2013-01-31')
and FieldName = 'Tracker #'
and BaseValue = '18570'
What object references are available for Tfs_DefaultCollection.dbo.tbl_ReleaseNoteDetails?
There is no 1to1 object reference in the TFS API, because you usually don't work on the structure like the database looks like.
What I understand from your description, you have the information you need in the changesets. In that case you could use the VersionControlServer to get the changesets and get the information from there.
TfsTeamProjectCollection tfsConnection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://TFS:8080/TFS/DefaultCollection"));
VersionControlServer sourceControl = (VersionControlServer)tfsConnection.GetService(typeof(VersionControlServer));
IEnumerable changesets = sourceControl.QueryHistory(ServerPath, VersionSpec.Latest, 0, RecursionType.Full, null, new DateVersionSpec (new DateTime(2013,1,1)), new DateVersionSpec (new DateTime(2013,1,31)), Int32.MaxValue, true, false);
foreach (Changeset change in changesets)
{
// check where the information is stored in the changeset, may change.Changes
}
Just an idea to get in the right direction.
If you want easy querying and searching, you're better off creating a new work item field and associate the work item during checkin. Work items have complete querying capabilities in the UI and are even transported to the Reporting warehouse.
You could retrieve the history for a specific folder, date range etc using a QueryHistoryParameters object and then iterate over the CheckinNotes to filter:
var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
new Uri("http://localhost:8080/tfs/DefaultCollection"));
var versionControlServer = projectCollection.GetService<VersionControlServer>();
var query = new QueryHistoryParameters("$/Scrum/**", RecursionType.Full);
var changesets = server.QueryHistory(query);
changesets.Where(cs => cs.CheckinNotes.Any(note => note.PropertyName == "Tracker #"
&& note.Value == "18570"))
This is not going to be terribly fast. To get a quick solution, use work item association.

Resources