Create Work Item API Request – Code Example

The examples below provide code for creating a Work Item.

Prerequisites

Before making an 'create work item' request, you must have an access token to sign the request. You can retrieve an access token using one of the following flows:

Additionally, you must have the following items in the tenancy in which you are creating the work item

  • A work group  – the ID of a work group in your tenancy is required when making the request
  • A template package – the ID of a template package uploaded to your tenancy is required when making the request

Code Examples

C#

The example below assumes that you have already retrieved an access token and are passing it to the method in the token parameter. This example also includes HotDocs answer XML, which you should validate before sending the request.


using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace HotDocsAdvanceApiExamples
{
    public class WorkItemRequest
    {
        public async Task<HttpStatusCode> CreateWorkItem(string token)
        {
	          // Create the 'create work item' request  
            var workItemId = Guid.NewGuid();
            var request = CreateHttpRequestMessage(workItemId);
            //Send create user request to Advance
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                var response = await client.SendAsync(request);
                return response.StatusCode;
            }
        }
        
        private HttpRequestMessage CreateHttpRequestMessage(Guid workItemId)
        {
          // Create the request for the 'create work item' API endpoint
            var createWorkItemUrl = string.Format("https://yourtenancy.yourorganization.com/HdaApi/rest/v1.1/WorkItems/{0}",
 workItemId);
            return new HttpRequestMessage
            {
                RequestUri = new Uri(createWorkItemUrl),
                Method = HttpMethod.Put,
                Content = GetRequestContent()
            };
        }
        
        private StringContent GetRequestContent()
        {
          // Generate the JSON request body required by the 'create user' method
            var json = "{\"name\": \"Example Contract A\",\"templatePackageId\": \"03000000-0000-0000-0000-000000000003\",
\"workGroupId\": \"09000000-0000-0000-0000-000000000001\",\"answerXml\": \"<answerset title="\'\'" version="\'1.1\'" 
usemanglednames="\'false\'"><answer name="\'ClientName\'"><textvalue>Bob Sherunkle</textvalue></answer></answerset>\",
\"assembleImmediately\": true, \"completeAssemblySession\": false, \"isPrivateToOwner\": true}";
            return new StringContent(json, Encoding.UTF8, "application/json");
        }
    }
}

Powershell

The example below assumes that you have already retrieved an access token and assigned it to the variable $token. This example also includes HotDocs answer XML, which you should validate before sending the request.

$body = @{
    "name" = "Example Work Item ABC"
    "description" = "A new work item."
    "templatePackageId"= "03040300-0520-1210-6420-00c009000q04"
    "workGroupId"= "09000000-0000-0000-0000-000000000001"
    "answerXml"= "<answerset title="" version="1.1" usemanglednames="false"><answer name="ClientName"><textvalue>Bob Sherunkle</textvalue></answer></answerset>"
} | ConvertTo-Json
$header = @{
 "Accept"="application/json"
 "Content-Type"="application/json"
 "Authorization" = "bearer " + $token
}
$createWorkItemResult = Invoke-RestMethod -Uri "https://yourtenancy.yourorganization.com/HdaApi/rest/v1/WorkItems/2d9f3d10-6fdb-494f-9929-955515c6f121" -Method 'Put' -Body $body -Headers $header
Write-Host $createWorkItemResult

JavaScript

// The data to be sent with the request
var data = {
    "name": "Work Item XYZ",
    "templatePackageId": "2bc68d7c-bf9f-4fce-8632-33595cb669c0",
    "workGroupId": "09000000-0000-0000-0000-000000000001",
    "answerXml": "<AnswerSet title='' version='1.1' useMangledNames='false'><Answer name='ClientName'><TextValue>Bob Sherunkle</TextValue></Answer></AnswerSet>"
};
// Create work item request URL
var url = "https://yourtenancy.yourorganization.com/HdaApi/rest/v1/WorkItems/f7166f04-02d2-40ac-9e05-4c3dd8632591";
// Create the request 
var xhr = new XMLHttpRequest(); xhr.open("PUT", url, true); xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); xhr.setRequestHeader('Authorization', 'Bearer ' + token); 
// Send the request 
xhr.send(JSON.stringify(data));