2

I am getting WorkItems from TFS using this code:

private static List<WorkItem> GetTfsWorkItems(string projectName)
{
    Uri URI = new Uri("...", UriKind.Absolute);
    TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(URI);
    WorkItemStore store = new WorkItemStore(tpc);
    string query = $@"SELECT *
                      FROM WorkItems
                      WHERE [System.TeamProject] = '{projectName}'";
    return store.Query(query).Cast<WorkItem>().ToList();
}

WorkItem have fields "CreatedBy" and "ChangedBy" both of which are strings with user's DisplayName. I am looking for way to get user's AccountName (or Sid) instead of it. Thanks in advance!

I.Dukhin
  • 23
  • 4

1 Answers1

1

It's not able straightforward to get the AccountName. Fields with username values like the CreatedBy/ChangedBy field are stored in the TFS database as a reference to an identity, not simply as the display name.

However querying the list of accounts by display name is not supported. As a workaround you have to dig into the account list and match the display name to an identity. That identity has your AccountName.

With help of TeamFoundationIdentity, you could get what you need. Take a look at below tutorials with more details:

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • Thanks for your answer! Querying the list of accounts by display name is not supported because it's unsafe: there can be two users with the same DisplayName. I took advantage of your recommendations, but I'm still looking for a direct link between WorkItem's fields and TeamFoundationIdentities. – I.Dukhin Jun 26 '17 at 07:06
  • @I.Dukhin Which you want just not part of the TFS design for now. You could also dig in the TFS database for it which may do the trick.Take a look at this tutorial-- [How to query Work Items using SQL on the Relational Warehouse](https://blogs.msdn.microsoft.com/granth/2010/05/09/tfs2010-how-to-query-work-items-using-sql-on-the-relational-warehouse/). You could also add a [uservocie](https://visualstudio.uservoice.com/forums/330519-team-services), TFS PM will kindly review your suggestion. – PatrickLu-MSFT Jun 26 '17 at 07:36