Blog Archive

All blog posts

Search Blog

Be our friend

Populate Dynamic Tabs in a Template

Posted October 3, 2011

A recent question on the DocuSign forums asked how to populate pre-placed Tabs in a Template from the API. This is a pretty common use-case for anyone who has a Document they want to mock-up in the Console and has dynamic values that can be updated from the API. 


The first step is to create a new Template from the Console. (Log in, click Templates on the left, then the Actions menu on the top-right). 


We'll upload a blank PDF for this test. 


Next we add a Signer Role (click 'Role' at the top of the page). 



Now, drag over a Data Field and a Signature Tab. 



Click on the Data Field, then click the Gear icon that pops up next to it. This will bring up the options for the Tab. Change the Label of the Tab to "TestField"


On the bottom of the page, click "Go Back." Enter a name for the Template and copy the Template ID. Click "Save Changes" at the bottom of the page and you'll be dropped back into the Console. 


Now, from our application we'll write some PHP code that will populate the "TestField" with some custom text. 

We are using code from the DocuSign SDK Sample on GitHub, so first you'll need to download that. After you've got it, create a new file in the PHP/DocuSignSample directory and put the following (commented) code in there. Make sure to replace our temporary values (Template ID, TabLabel) with the ones you created! 

<?php

// Includes
include_once 'include/session.php'; // initializes session and provides
include_once 'api/APIService.php';
include 'include/utils.php';

// Check to make sure we're logged in
loginCheck();

// Envelope variables [CHANGE THESE]
$Subject = 'Subject of the email';
$EmailBlurb = 'email text';
$templateID = 'TEMPLATE_ID_HERE';

// Recipient and Tab variables [CHANGE THESE]
$UserName = 'FULLNAME_HERE';
$Email = 'EMAIL_HERE';
$RoleName = 'Signer'; // The Role you defined in the Console
$TabLabel = 'TestField'; // TabLabel that you created
$TabValue = 'Awesome'; // Value that you want to use to pre-populate

// Create envelope information
$envinfo = new EnvelopeInformation();
$envinfo->Subject = $Subject;
$envinfo->EmailBlurb = $EmailBlurb;
$envinfo->AccountId = $_SESSION["AccountID"];

// Create 1 Recipient
$r = new Recipient();
$r->UserName = $UserName;
$r->Email = $Email;
$r->ID = '1';
$r->RoleName = $RoleName;
$r->Type = RecipientTypeCode::Signer;
$recipients = array($r);

// Create the ReferenceRoleAssignment for Recipient
$ra = new TemplateReferenceRoleAssignment();
$ra->RecipientID = $r->ID;
$ra->RoleName = $r->RoleName;
$roleAssignments = array($ra);

// Construct the template reference
$tref = new TemplateReference();
$tref->TemplateLocation = TemplateLocationCode::Server;
$tref->Template = $templateID;
$tref->RoleAssignments = $roleAssignments;

// Construct a TemplateReferenceFieldDataDataValue value
$fd = new TemplateReferenceFieldDataDataValue();
$fd->TabLabel = $TabLabel;
$fd->Value = $TabValue;

// Add array to TemplateReferenceFieldData->DataValues (per WSDL and APIService.php)
$fdd = new TemplateReferenceFieldData();
$fdd->DataValues = array($fd);

$tref->FieldData = $fdd;
$templateReferences = array($tref);

// Send the Envelope
$api = getAPI();

$csParams = new CreateEnvelopeFromTemplates();
$csParams->EnvelopeInformation = $envinfo;
$csParams->Recipients = $recipients;
$csParams->TemplateReferences = $templateReferences;
$csParams->ActivateEnvelope = true; // Email the Envelope
try {
$status = $api->CreateEnvelopeFromTemplates($csParams)->CreateEnvelopeFromTemplatesResult;
if ($status->Status == EnvelopeStatusCode::Sent) {
echo "Envelope Sent Successfully";
exit;
}
} catch (SoapFault $e) {
$_SESSION["errorMessage"] = array($e, $api->__getLastRequest(), $csParams);
header("Location: error.php");
}

?>

After you've copied the code over and updated the values, run the sample code (you'll be prompted to log-in first), check your email for the Sent Envelope, and view your custom value in the Document! 

That is it! Now even if you have multiple Tabs with the same required value, you only have to give them the same TabLabel and can populate all of them at once! 

Did you know DocuSign gives you a Free Developer Account to try the API? Sign up for one and ask us questions on our Community Forums

DocuSign is hiring! If you're interested in engineering more ways for people to easily use the industry standard for electronic signatures, contact mikeb@docusign.com

I agree to the Terms and Conditions