JavaScript API Example 4: Get Answer XML from the Interview
In some scenarios, you may wish to retrieve the HotDocs Answer XML from the interview while it is in progress. For example, you may decided to write a custom function for posting answer data back to the server the interview is only partially complete.
In this example, you will:
- Use the HotDocs JavaScript API to get the answer XML from the interview Write the answer XML to the console.
Full source code for this example is available at the bottom of the page.
Example Source Code on GitHub
The JavaScriptAPIExamples example project is available on GitHub, in the HotDocs-Samples repository.
1. Create a new ActionResult in the Controller
To display the View created above when the project is run, create a new ActionResult in the HomeController for the current project.
Add a new ActionResult method for this example:
public ActionResult Example4()
{
Util.SetPersistentEncryptionKey( "ExampleKey");
var interviewFragment = GetInterviewFragment();
var model = new InterviewModel { InterviewFragment = interviewFragment };
return View( model);
}
The contents of the ActionResult are the same as the ActionResult created for example 1. This will create an interview fragment and pass it to the browser. In this example, however, the interview fragment will be passed to a different view, which you will create in the next step.
2. Create a new View
As in JavaScript API Example 1, you need to create a new View for this example.
To create a new View in Visual Studio:
- Expand the Views folder in the current project.
- Right-click the Views > Home folder and select Add > View from the drop-down list. The Add View dialog appears.
- In the Add View dialog, enter the following data:
- View Name: Example4
- Click Add. The new View appears in the Views > Home folder.
You will edit this file later in the example.
3. Get the Answer XML from the Interview
Create a new Function, GetHotDocsAnswerXml:
function GetHotDocsAnswerXml(){
}
Next, add a call to the HotDocs JavaScript API method, GetXMLAnswers. This method retrieves the answer XML from the interview:
var answerXml = HDAPI.GetXMLAnswers();
Finally, write the XML data to the browser's developer tools console:
console.log( answerXml);
The final function looks like this:
function GetHotDocsAnswerXml(){
var answerXml = HDAPI.GetXMLAnswers();
console.log( answerXml);
}
4. Add a Button to the View
To invoke the new function above, add a button to the View that calls GetHotDocsAnswerXml when clicked:
<input type="button" value="Get Answer XML" onclick="GetHotDocsAnswerXml();"/>
You will use this button when testing the custom button.
5. Test
To run the project and test the answer XML retrieval:
- Set the project as a Startup Project (Right-click the JavaScriptAPIExamples project in Visual Studio, select Startup Project from the drop-down menu)
- Press F5 to run the Project. The browser opens.
- Add /Home/Example4 to the URL in the browser. The interview loads.
- When the interview has finished loading, enter answer data into the interview fields.
- Press F12 to open the Developer Tools.
- Select the Console tab.
- Click the Get Answer XML button on the interview page. The Answer XML appears in the console.
Source Code (C#)
In the HomeController:
public ActionResult Example4()
{
Util.SetPersistentEncryptionKey( "ExampleKey");
var interviewFragment = GetInterviewFragment();
var model = new InterviewModel { InterviewFragment = interviewFragment };
return View( model);
}
In the Example4 view:
@model JavaScriptAPIExamples.Models.InterviewModel
@{
ViewBag.Title = "Example 4";
ViewBag.SubTitle = "Retrieving Answer XML from the Interview";
}
<h2>@ViewBag.Title</h2>
<h3>@ViewBag.SubTitle</h3>
<script type="text/javascript">
/* Example 4 Javascript functions */
function GetHotDocsAnswerXml() {
var answerXml = HDAPI.GetXMLAnswers();
console.log( answerXml);
}
</script>
<div>
<!-- Example 4 Buttons -->
<input type="button" onclick="GetHotDocsAnswerXml()" value="Get Answer XML"/><br/>
<!-- HotDocs Interview -->
@Html.Raw( Model.InterviewFragment)
</div>