Search code examples
c#.netc#-4.0tfstfs-sdk

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


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?


Solution

  • 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();