0

Code:

// To create a User Story

    var collectionUri = new Uri(txtTFS.Text);
    var tpc = new TfsTeamProjectCollection(collectionUri);
    var workItemStore = tpc.GetService<WorkItemStore>();
    var teamProject = workItemStore.Projects[txtSelectedProject.Text];

    var typeWorkItem = ConfigurationManager.AppSettings["WorkItemType"];
    var workItemType = teamProject.WorkItemTypes[typeWorkItem];

    var userStory = new WorkItem(workItemType)
    {
        Title = "Test Title",
        Description = "Test Description",
        IterationPath = "xx\\yy\\zz",
        AreaPath = "xxx\\yyy\\zzz",
        State = "New",
        // "AssignedTo" field not populated here...
    };

    // Save the new user story.
    userStory.Save();

How to set value in "AssignedTo" field in TFS programatically while creating a UserStory?

Earth
  • 3,477
  • 6
  • 37
  • 78

1 Answers1

2

Only the fields that are on every work item type have their own property on the WorkItem class.

You should use the WorkItem.Fields property to access any field that are not properties.

userStory.Fields["System.AssignedTo"].Value = "JJJ";

You cannot really use properties with indexers inside object intialiser syntax so you will have to have then on a new line before .Save();

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • @DaveShaw I tried this for the TargetRelease, StoryPoints and similar fields that do not have properties, and my program crashes and says the field definition doesn't exist, Do you have any idea how I can set these? Thanks – Alex Mar 04 '16 at 09:41
  • 1
    @Alex The field names are not in `System.`. If you hover over the label on the Work Item form (web or VS) you will see a full field name like "Story Points" - you can use that instead. You can also list all fields with `witadmin` command line tool with the argument of `listfields`. – DaveShaw Mar 04 '16 at 10:57
  • @DaveShaw Thanks for your reply, I will test this out later on today, much appreciated – Alex Mar 04 '16 at 11:41
  • 1
    @DaveShaw Just to let you know, your answer was very helpful, thanks again – Alex Mar 08 '16 at 10:10