Using @CurrentIteration with TFS API

@CurrentIteration is neat macro I use when I created saved queries in TFS. For example, I recently needed to write a query that fetched all current bugs in the sprint:


This works great in the TFS web interface. The main reason for me to create this saved query was however was to be able to access it via the TFS API. So I started like this:


using (var workItemTrackingHttpClient = 
       new WorkItemTrackingHttpClient(new Uri("http://mytfs/tfs/Plato"), 
       new VssCredentials()))
{
    try
    {
        var items = workItemTrackingHttpClient.QueryByIdAsync(
                     new Guid("25c6e737-d0ce-416f-9f7a-3cb78959334d")).Result.WorkItems;
    }
    catch (Exception ex)
    {

        
    }
}


This returned the following error:

System.AggregateException: One or more errors occurred. ---> Microsoft.VisualStudio.Services.Common.VssServiceException: VS402612: 
The macro '@CurrentIteration' is not supported without a team context.
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__52.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__50.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__47`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__28`1.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at MakeTestBugs.Program.Main(String[] args) in C:\Users\mikyas.wolde\Documents\Visual Studio 2012\Projects\MakeTFSITems\MakeTestBugs\Program.cs:line 27
---> (Inner Exception #0) Microsoft.VisualStudio.Services.Common.VssServiceException: VS402612: The macro '@CurrentIteration' is not supported without a team context.
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__52.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__50.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__47`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__28`1.MoveNext()

I was getting an macro '@CurrentIteration' is not supported without a team context error. I solved that problem by doing this:

  var items = workItemTrackingHttpClient.QueryByIdAsync(new TeamContext("720"), new Guid("25c6e737-d0ce-416f-9f7a-3cb78959334d")).Result.WorkItems;

I solved the problem by calling an overloaded method of QueryByIdAsync that takes a TeamContext, which is initialized with the name of the project, in my case '720'.

Code samples on github are a great resource for the TFS API.

Comments

Popular posts from this blog

Adding CKEditor 4 to Angular