Search code examples
powershelltfsazure-devopstfs-power-toolsazure-devops-rest-api

How do I format HTML Fields for TFS / VSTS workitem queries


I am trying to use the Team Foundation Server Power Tools (tfpt) to create workitems programmatically. I need to create many test cases this way. The power tools are largely undocumented unfortunately but I have traced it down to one last remaining bits. I need to be able to create test steps alongside the test case. This is done with a field called Steps=

For Example: /fields: "Title=My Title;Steps="

Now digging as far as I could in the field explorer, the text that follows steps has to be "HTML formatted" But I have no idea what Microsoft's definition of HTML is and what the tags should be in order to properly serve the data.

Any help is much appreciated


Solution

  • It is general HTML formatted value, for example <div></div>, <B></B>. The detail value will be encoded. You can get encoded value via online tool.

    On the other hand, there is additional information indicate test step actions, for example: <step id=”4” type=”ActionStep”> <parameterizedString isformatted="true"></ parameterizedString></step>.

    A simple step value:

    <steps id=\"0\" last=\"4\"><step id=\"2\" type=\"ActionStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;st&lt;B&gt;ep&lt;/B&gt;1&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description /></step><step id=\"3\" type=\"ActionStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;st&lt;I&gt;ep&lt;/I&gt;2&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description /></step><step id=\"4\" type=\"ActionStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;s&lt;U&gt;te&lt;/U&gt;p3&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description /></step></steps> 
    

    enter image description here

    I recommend that you can create test case by using TFS/VSTS API (Client SDK or Rest API)

    C# code:

    NetworkCredential cred = new NetworkCredential("XXX", "XXX");
         TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("[collection url]"), cred);
                tpc.EnsureAuthenticated();
    
                var workItemStore = (WorkItemStore)tpc.GetService(typeof(WorkItemStore));
                Project teamproject = workItemStore.Projects["ScrumStarain"];
                WorkItemType testCaseType = teamproject.WorkItemTypes["Test Case"];
    
                WorkItem testCase = new WorkItem(testCaseType)
                {
                    Title="TestCaseApi2"
                };
                testCase.Fields["Microsoft.VSTS.TCM.Steps"].Value = "[previous sample value]";
                testCase.Save();
    

    Also, you can get a test case step value by using this code:

    var wit = workItemStore.GetWorkItem(408);
    object stepValue = wit.Fields["Microsoft.VSTS.TCM.Steps"].Value;
    

    Rest API: Create a work item

    Body sample:

    [
      {
        "op": "add",
        "path": "/fields/System.Title",
        "value": "newTestcase"
      },
       {
        "op": "add",
        "path": "/fields/Microsoft.VSTS.TCM.Steps",
        "value": "<steps id=\"0\" last=\"4\"><step id=\"2\" type=\"ActionStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;st&lt;B&gt;ep&lt;/B&gt;1&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description /></step><step id=\"3\" type=\"ActionStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;st&lt;I&gt;ep&lt;/I&gt;2&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description /></step><step id=\"4\" type=\"ActionStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;s&lt;U&gt;te&lt;/U&gt;p3&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description /></step></steps>"
      }
    ]