title: How can I retrieve a list of workitems from TFS in C#?
body: <p>I'm trying to write a project reporting tool in WPF / C#.  I want to access all the project names on our TFS (Team Foundation Server), and then display statistics for each work item in a given project.</p>
  
  <p>I've got the project names, but getting the actual work items is what's giving me a hard time.  Here's what I've got so far:</p>
  
  <pre><code>public const string tfsLocation = "http://whatever";
  
  // get the top list of project names from the team foundation server
  public List&lt;string&gt; LoadProjectList()
  {
      var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsLocation));
      var workItemStore = new WorkItemStore(tpc);
      var projects = (from Project project in workItemStore.Projects select project.Name).ToList();
  
      return projects;
  }
  
  public string GetProjectInfo(string targetProject)
  {
      string info = String.Empty;
  
      var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsLocation));
      var workItemStore = new WorkItemStore(tpc);
  
      foreach (Project project in workItemStore.Projects)
      {
          if (project.Name == targetProject)
          {
              info += String.Format("Project: {0}\n\n", project.Name);
  
              info += "Work Item Types:\n";
              foreach (WorkItemType item in project.WorkItemTypes)
              {
                  info += String.Format("-   {0}\n", item.Name);
                  info += String.Format("    -   Description: {0}\n", item.Description);
                  info +=               "    -   Field Definitions:\n";
  
                  foreach (FieldDefinition field in item.FieldDefinitions)
                  {
                      info += String.Format("        -   {0}\n", field.Name);
                  }
                  info += "\n";
              }
          }
      }
  
      return info;
  }
  </code></pre>
  
  <p>GetProjectInfo sends back some helpful info about what's in each project, but so far it looks like I'm only seeing the <em>definitions</em> of what the WorkItems consist of, and not the <em>actual</em> WorkItems themselves.  I think the programming I've written is looking in the wrong place.</p>
  
  <p>From Microsoft's definition of WorkItem,
  (<a href="http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.workitemtracking.client.workitem.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.workitemtracking.client.workitem.aspx</a>)
  it looks like it's inside WorkItemTracking.Client, but not inside the WorkItemStore, and I'm not sure where to go to access it.</p>
  
  <p><strong>FINAL VERSION:</strong></p>
  
  <p>Here's the updated version of my function, after referencing the below answer.  This just returns a long string of the work item names with new lines between, for printing out, which is all I'm trying to get working (for now).</p>
  
  <pre><code>public string GetProjectInfo(string targetProject)
  {
      string info = String.Empty;
  
      var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsLocation));
      WorkItemStore workItemStore = new WorkItemStore(tpc);
  
      Query query = new Query(workItemStore, "SELECT * FROM WorkItems WHERE [System.TeamProject] = @project", new Dictionary&lt;string, string&gt;() { { "project", targetProject } });
  
      WorkItemCollection wic = query.RunQuery();
  
      foreach (WorkItem item in wic)
      {
          info += String.Format("{0}\n", item.Title);
      }
  
      return info;
  }
  </code></pre>