Create User API Request – Code Example

The examples below provide code for creating a User.

In this Topic Hide

  1. Prerequisites
  2. Code Examples
    1. C#
    2. Powershell
    3. JavaScript

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:

Code Examples

C#

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 UserRequest     {         public async Task<HttpStatusCode> CreateUser(string token)         {             // Create the 'create user' request               var userId = Guid.NewGuid();             var request = CreateHttpRequestMessage(userId);
            //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 userId)         {             // Create the request for the 'create user' API endpoint             var createUserUrl = string.Format("https://yourtenancy.yourorganization.com/HdaApi/rest/v1.1/Users/{0}", userId);
            return new HttpRequestMessage             {                 RequestUri = new Uri(createUserUrl),                 Method = HttpMethod.Post,                 Content = GetRequestContent()             };         }
        private StringContent GetRequestContent()         {             // Generate the JSON request body required by the 'create user' method             var json = "{ 'firstName': 'Emma', 'lastName': 'MacDiarmid', 'emailAddress': 'emma.macdiarmid@example.com', 'roles': [ 'SiteAdministrator' ], 'sendEmail': 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.


$body = @{
    "firstName" = "Emma"
    "lastName" = "MacDiarmid"
    "emailAddress" = "emma.macdiarmid@example.com"
    "roles" = @("SiteAdministrator")
} | ConvertTo-Json
$header = @{  "Accept"="application/json"  "Content-Type"="application/json"  "Authorization" = "bearer " + $token }
$createUserResult = Invoke-RestMethod -Uri "https://yourtenancy.yourorganization.com/HdaApi/rest/v1/Users/5b505239-557c-5570-b53f-7cc7f46445bd" -Method 'Post' -Body $body -Headers $header
Write-Host $createUserResult

JavaScript

The example below assumes that you have already retrieved an access token using the Implicit flow, returning the token as a fragment.

// You must omit the # character from the beginning of the token.
var token = window.location.hash.substr(1);
// The data to be sent with the request var data = {     "firstName": "Bob",     "lastName": "Sherunkle",     "emailAddress": "bob.sherunkle@example.com",     "roles": [         "SiteAdministrator"     ],     "sendEmail": true };
// Create User request URL var url = "https://yourtenancy.yourorganization.com/HdaApi/rest/v1/Users/3ca8dc6a-8523-4d89-8c1d-b7306651f7a2"; // Create the request var xhr = new XMLHttpRequest(); xhr.open("POST", url, true); xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); xhr.setRequestHeader('Authorization', 'Bearer ' + token);
// Send the request xhr.send(JSON.stringify(data));