The examples below provide code for creating an assembly session, displaying an interview, and finishing an assembly session.
Before making an 'display interview' 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
The example below contains code for the following 'Using the API' topics:
This code example assumes that you are using .Net Framework 4.6 and a new ASP.NET Web Application project. The HomeController.cs and Index.cshtml files should automatically appear in your project. You will need to add the following files to your project manually:
using System; using System.Web.Mvc; using System.Threading.Tasks; using AdvanceExampleApplication.Models;
namespace AdvanceExampleApplication.Controllers { public class HomeController : Controller { public async Task<ActionResult> Index() { // You should have implemented these methods in the 'Getting an Access Token using the Resource Owner flow' and 'Begin an assembly session' topics var token = await new ResourceOwnerAuthorization().GetResourceOwnerToken(); var assemblySessionData = AssemblySessionHandler.BeginAssemblySession(token);
// The ID of the work item; this must be the same as the one used in 'BeginAssemblySession' var workItemId = Guid.Parse("a1e0e795-5a51-44f5-8fe2-e85a61dbf3c2");
// Create the view model, containing the data required to create the interview var interviewViewModel = new InterviewViewModel { ServiceMetadataUrl = assemblySessionData.ServiceMetadataUrl, InterviewJsUrl = assemblySessionData.InterviewJsUrl, CoreSessionId = assemblySessionData.CoreSessionId, WorkItemId = workItemId, Token = token };
return View(interviewViewModel); } }
[HttpPost] public async Task CompleteAssemblySession(string workItemId, string token) { await AssemblySessionHandler.CompleteAssemblySession(Guid.Parse(workItemId), token); }
public ActionResult LandingPage() { // Landing page for the user when they finish the interview; this endpoint is used in InterviewOptions, on the interview page. return View(); } }
using System; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json;
namespace AdvanceExampleApplication { public static class AssemblySessionHandler { public static async Task<assemblysessiondto> BeginAssemblySession(string token) { // ID of a work item for which you want to create the assembly session; // it must not have an in-progress assembly session var workItemId = Guid.Parse("a1e0e795-5a51-44f5-8fe2-e85a61dbf3c2");
// Create the request var request = CreateHttpRequestMessage(workItemId);
// Send the begin assembly session request to Advance using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var response = await client.SendAsync(request); return JsonConvert.DeserializeObject<assemblysessiondto>(await response.Content.ReadAsStringAsync()); } }
private static HttpRequestMessage CreateHttpRequestMessage(Guid workItemId) { var assemblySessionId = Guid.NewGuid(); var assemblySessionUrl = $"https://tenancy1.server.yourorganization.com/HdaApi/rest/v1.1/WorkItems/{workItemId}/Versions/{assemblySessionId}";
var body = new { forceCreate = true, answerXml = string.Empty };
return new HttpRequestMessage { RequestUri = new Uri(assemblySessionUrl), Method = HttpMethod.Put, Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json") }; } } }
namespace AdvanceExampleApplication
{
public class AssemblySessionDto
{
public string InterviewJsUrl { get; set; }
public string HdaSessionId { get; set; }
public string CoreSessionId { get; set; }
public string ServiceMetadataUrl { get; set; }
}
}
using System;
namespace AdvanceExampleApplication.Models
{
public class InterviewViewModel
{
public string ServiceMetadataUrl { get; set; }
public string InterviewJsUrl { get; set; }
public string CoreSessionId { get; set; }
public Guid WorkItemId { get; set; }
public string Token { get; set; }
}
}
@model AdvanceExampleApplication.Models.InterviewViewModel
// Container div for the interview <div id="hdMainDiv"></div>
// Interview JavaScript reference <script type="text/javascript" src="https://yourorganization.com/AssemblyService/scripts/interview.js"></script>
<script type="text/javascript">
// Configure the interview options; this registers the 'OnSessionComplete' event that fires when the user clicks the // interview 'Finish' button. It then posts the data from the interview to your 'CompleteAssemblySession' endpoint. // You will add the 'CompleteAssemblySession' endpoint in the next topic, 'Completing an Assembly Session'. // Note that the value for the 'Container' property must match the ID of your interview container div at the top of the page. var interviewOptions = { Container: "hdMainDiv", Theme: "default.css", OnInit: () => { HD$.RegisterHandler("OnSessionComplete", (e) => { $.post("CompleteAssemblySession", { Token: "@Model.Token", WorkItemId: "@Model.WorkItemId" }, function() {
// Redirect the user to your application's 'Interview Complete' landing page window.location.href = "/Home/LandingPage"; }); }); } };
// Attach the assembly session to the interview HD$.AttachSession( "@Model.CoreSessionId", "@Model.ServiceMetadataUrl", interviewOptions ); </script>
<h2>Assembly Complete!</h2>