5

I want to create a new work item in TFS using the SDK, and I'd like to set the item's effort estimates. My code at the moment looks like this

    var coll = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://galaxy:8080/tfs/crisp"));

    var workItemService = coll.GetService<WorkItemStore>();

    var parent = workItemService.GetWorkItem(parentWorkItemId);

    WorkItemType workItemType =parent.Project.WorkItemTypes
            .Cast<WorkItemType>()
            .First(candidateType => candidateType.Name.Equals("Task"));



    WorkItem item = workItemType.NewWorkItem();
    item.Title = work.Name;


    //Set effort estimate here

    workItemService.BatchSave(new WorkItem[]{ item });

But there doesn't seem to be anything on the interface for WorkItem which allows me to set an effort estimate. Does anyone know how this is done?

Henrik
  • 23,186
  • 6
  • 42
  • 92
Ceilingfish
  • 5,397
  • 4
  • 44
  • 71

1 Answers1

7

Turns out it's done by using the [] operator on the WorkItem object.

var coll = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://galaxy:8080/tfs/crisp"), new UICredentialsProvider());

var workItemService = coll.GetService<WorkItemStore>();

var parent = workItemService.GetWorkItem(parentWorkItemId);

WorkItemType workItemType =parent.Project.WorkItemTypes
            .Cast<WorkItemType>()
            .First(candidateType => candidateType.Name.Equals("Task"));

WorkItem item = workItemType.NewWorkItem();
item.Title = "A name";

item["Original Estimate"] = duration.TotalHours;
item["Completed Work"] = duration.TotalHours;
item["Remaining Work"] = 0.0;

int workItemId = item.Save();
granth
  • 8,919
  • 1
  • 43
  • 60
Ceilingfish
  • 5,397
  • 4
  • 44
  • 71
  • 1
    You can also refer to the fields using their 'reference name', e.g. 'Original Estimate' = 'Microsoft.VSTS.Scheduling.OriginalEstimate'. – granth Jan 17 '12 at 11:29
  • 2
    I modified the answer and added 'new UICredentialsProvider()' to the GetTeamProjectCollection() call. This will display a login prompt if the current user doesn't have access. – granth Jan 17 '12 at 11:31
  • 1
    Also, instead of using workItemService.BatchSave() - if you're only saving one work item, you may as well use item.Save(). Be careful when using BatchSave(), as you get an array of errors back that you have to handle, otherwise a work item save will 'silently' fail. – granth Jan 17 '12 at 11:32