0

I am trying to create a task / work item in Visual Studio Team Services (previously Visual Studio Online). My code fails because "tfsStore" returns null value causing the exception to be thrown.

NetworkCredential myNetCredentials = new NetworkCredential("*****", "******");
ICredentials myCredentials = (ICredentials)myNetCredentials;
Uri tfsUri = new Uri("https://*****.visualstudio.com/DefaultCollection/");
var tfsServer = new TfsTeamProjectCollection(tfsUri, myCredentials);
tfsServer.EnsureAuthenticated();
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(tfsUri);
WorkItemStore tfsStore = tfsServer.GetService<WorkItemStore>(); 
if (tfsStore == null)
{
    throw new Exception("Cannot initialize TFS Store");
}
Project teamProject = tfsStore.Projects["******"];

I would appreciate any useful tips as to how to resolve the error. Thanks!

Esther Fan - MSFT
  • 8,276
  • 4
  • 27
  • 25
JayHawk
  • 275
  • 1
  • 5
  • 15

1 Answers1

0

I've tested the code snippet below on my side, which can create a Task work item successfully, you may have a try:

NetworkCredential myNetCredentials = new NetworkCredential("******", "******");
TfsTeamProjectCollection tfsUri = new TfsTeamProjectCollection(new Uri("https://*****.visualstudio.com/DefaultCollection"), myNetCredentials);
WorkItemStore tfsStore = tfsUri.GetService<WorkItemStore>();
         if (tfsStore == null)
            {
                throw new Exception("Cannot initialize TFS Store");
            }

         Project teamProject = tfsStore.Projects["*****"];
         WorkItemType workItemType = teamProject.WorkItemTypes["Task"];
         WorkItem Task = new WorkItem(workItemType)
            {
                Title = "APITask",
            };
            Task.Save();

Including .net api, you can also use REST API, which is simply:

PATCH https://{instance}/DefaultCollection/{project}/_apis/wit/workitems/${workItemTypeName}?api-version={version}
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • I still get Exception "Cannot initialize TFS store" since tfsStore is null. Thx! – JayHawk Jun 13 '16 at 16:37
  • Uh...This code works fine on my side. Did this code connect to VSTS successfully? – Cece Dong - MSFT Jun 14 '16 at 09:20
  • Yes. I am able to successfully connect to VSTS. However, its failing to instantiate the WorkItemStore. I guess it doesn't matter since I was able to use the REST API to get the code to work. Thanks! – JayHawk Jun 14 '16 at 19:35