blog

eSignLive How To: Creating a Template

Developer
|  December 22, 2015 Haris Haidary
esignlive man using laptop

Templates enable a sender to quickly create a new document package by using a previous one as blueprint. It is a great tool if you plan to send the same documents for signing on a regular basis. In this blog, I will show you how to create a template from scratch and from an existing package using the eSignLive Java SDK, .NET SDK, and REST API.

Templates in the UI

To begin with, you should locate the templates section in your UI. After logging into your eSignLive account, click on TEMPLATES in your toolbar. After running your code, you should find your saved templates here.

uitemplate

The Code

As I mentioned before, I will show you how to do this with the eSignLive Java SDK, .NET SDK, and REST API. You can go ahead and skip to the section which applies to you. I will cover the exact same information in every section. In my example, I will show how to create a template with one document and two signers. Full example code for this blog can be found in the Developer Community Code Share (Java, .NET, REST). [promotion id="14439"] JAVA SDK First, I will begin with the Java SDK. The sample code below creates a template from scratch, with one document and two signers. The first step is to create your eSignLive client. Make sure to replace the API_KEY, and API_URL placeholders with your values. Afterwards, you build your document package with the applicable properties. Once your package is built, you call on your TemplateService to create your template.

package com.esignlive.example;

import com.silanis.esl.sdk.DocumentPackage;
import com.silanis.esl.sdk.EslClient;
import com.silanis.esl.sdk.PackageId;
 
import static com.silanis.esl.sdk.builder.DocumentBuilder.newDocumentWithName;
import static com.silanis.esl.sdk.builder.PackageBuilder.newPackageNamed;
import static com.silanis.esl.sdk.builder.SignatureBuilder.signatureFor;
import static com.silanis.esl.sdk.builder.SignerBuilder.newSignerWithEmail;

public class Templates {
	
    public static final String API_KEY = "your_api_key";
    public static final String API_URL = "https://sandbox.e-signlive.com/api"; 
    // USE https://apps.e-signlive.com/api FOR PRODUCTION
 
    public static void main( String[] args ) {
    	
    	EslClient eslClient = new EslClient( API_KEY, API_URL );
    	
    	DocumentPackage documentPackage = newPackageNamed("My template from scratch Java SDK") 
    					.withSigner(newSignerPlaceholder(new Placeholder("PlaceholderId1"))) 
    					.withSigner(newSignerPlaceholder(new Placeholder("PlaceholderId2")))
    					.withDocument(newDocumentWithName("My Document") 
    							.fromFile("YOUR_FILE_PATH") 
    							.withSignature(signatureFor(new Placeholder("PlaceholderId1")) 
    									.onPage(0) 
    									.atPosition(100, 140))
    							.withSignature(signatureFor(new Placeholder("PlaceholderId2"))
    									.onPage(0) 
    									.atPosition(100, 205)))
    					.build();
    	
    	PackageId templateId = eslClient.getTemplateService().createTemplate(documentPackage);
    }

}

You can also create a template from an existing package. For this, you will need the packageId of your package.

PackageId packageId = new PackageId("d9355f04-9bb2-4219-b9fa-734f2650a939");
PackageId templateFromPackage = eslClient.getTemplateService().createTemplateFromPackage(packageId, "New Template From Package" );

.NET SDK Next, I will go over the .NET SDK. The sample code below creates a template from scratch, with one document and two signers. The first step is to create your eSignLive client. Make sure to replace the API_KEY, and API_URL placeholders with your values. Afterwards, you build your document package with the applicable properties. Once your package is built, you call on your TemplateService to create your template.

using System;
using System.IO;
using Silanis.ESL.SDK;
using Silanis.ESL.SDK.Builder;

namespace createTemplateFrom
{
    class createTemplateFrom
    {
        private static String apiUrl = "https://sandbox.e-signlive.com/api";
        // USE https://apps.e-signlive.com/api FOR PRODUCTION
        private static String apiKey = "your_api_key";

        public static void Main(string[] args)
        {
            EslClient eslClient = new EslClient(apiKey, apiUrl);

            FileStream fs = File.OpenRead("YOUR_FILE_PATH");

           DocumentPackage documentPackage = PackageBuilder
                    .NewPackageNamed("My template from scratch .NET SDK")
                    .WithSigner(SignerBuilder.NewSignerPlaceholder(new Placeholder("PlaceholderId1")))
                    .WithSigner(SignerBuilder.NewSignerPlaceholder(new Placeholder("PlaceholderId2")))
                    .WithDocument(DocumentBuilder.NewDocumentNamed("My Document")
                                  .FromStream(fs, DocumentType.PDF)
                                  .WithSignature(SignatureBuilder
                                       .SignatureFor(new Placeholder("PlaceholderId1"))
                                         .OnPage(0)
                                         .AtPosition(100, 140))
                                  .WithSignature(SignatureBuilder
                                       .SignatureFor(new Placeholder("PlaceholderId2"))
                                         .OnPage(0)
                                         .AtPosition(100, 205)))
                    .Build();

            PackageId templateId = eslClient.CreateTemplate(documentPackage);
        }
    }
}

You can also create a template from an existing package. To do this, you will need the packageId of your package.

PackageId packageId = new PackageId("d9355f04-9bb2-4219-b9fa-734f2650a939");
PackageId templateFromPackage = eslClient.CreateTemplateFromPackage(packageId, "New Template From Package");

REST API Finally, I will show you how to this with the REST API. Below is a C# class that makes a POST request to create a new template from scratch. The JSON request payload contains many empty properties that are not necessary for creating a new template. I included them to give you a list of options that are available and how they are parsed in the JSON.

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

namespace createTemplateREST
{
    public class createTemplateREST
    {
        public static void Main(string[] args)
        {

            string apiKey = "your_api_key";
            string url = "https://sandbox.e-signlive.com/api";

            string jsonString = "{ \"name\":\"My template from scratch REST\", \"description\":\"\", \"roles\":[ { \"name\":\"PlaceholderId1\", \"index\":0, \"id\":\"PlaceholderId1\", \"locked\":false, \"reassign\":false }, { \"name\":\"PlaceholderId2\", \"index\":0, \"id\":\"PlaceholderId2\", \"locked\":false, \"reassign\":false } ], \"type\":\"TEMPLATE\", \"due\":null, \"autocomplete\":true, \"emailMessage\":\"\", \"documents\":[ { \"approvals\":[ { \"role\":\"PlaceholderId1\", \"signed\":null, \"accepted\":null, \"data\":null, \"fields\":[ { \"page\":0, \"subtype\":\"FULLNAME\", \"width\":200, \"binding\":null, \"extract\":false, \"extractAnchor\":null, \"left\":175, \"top\":165, \"validation\":null, \"height\":50, \"data\":null, \"type\":\"SIGNATURE\", \"value\":\"\" }], \"name\":\"\" }, { \"role\":\"PlaceholderId2\", \"signed\":null, \"accepted\":null, \"data\":null, \"fields\":[ { \"page\":0, \"subtype\":\"FULLNAME\", \"width\":200, \"binding\":null, \"extract\":false, \"extractAnchor\":null, \"left\":550, \"top\":165, \"validation\":null, \"height\":50, \"data\":null, \"type\":\"SIGNATURE\", \"value\":\"\" }], \"name\":\"\" }], \"name\": \"YOUR_FILE_NAME\" }] }";
            
            StringContent jsonContent = new StringContent(jsonString, Encoding.UTF8, "application/json"); 

            byte[] fileByteArray = File.ReadAllBytes("YOUR_FILE_PATH");
            ByteArrayContent content = new ByteArrayContent(fileByteArray);

            content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
            content.Headers.ContentDisposition.Name = "\"file\"";
            content.Headers.ContentDisposition.FileName = "\"myDocument.pdf\"";
            content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

            MultipartFormDataContent form = new MultipartFormDataContent();
            form.Add(content, "\"file\"", "\"myDocument.pdf\"");
            form.Add(jsonContent, "\"payload\"");

            HttpClient myClient = new HttpClient();
            myClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", apiKey);
            myClient.DefaultRequestHeaders.Add("Accept", "application/json");

            var response = myClient.PostAsync(new Uri(url) + "/packages/", form).Result;
            Debug.WriteLine(response.Content.ReadAsStringAsync().Result);
        }
    }
}

Similarly, you can create a new template from an existing package. You will need the packageId of the package you are looking to clone.

string jsonString = "{\"name\":\"your_cloned_template\",\"description\":\"\",\"emailMessage\":\"\",\"autocomplete\":true,\"settings\":{\"ceremony\":{\"inPerson\":false}},\"type\":\"TEMPLATE\",\"sender\":{\"lastName\":\"Sender_Last_Name\",\"firstName\":\"Sender_First_Name\",\"email\":\"first.signer@example.com\"},\"visibility\":\"ACCOUNT\",\"due\":null,\"language\":\"en\"}";
StringContent jsonContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

HttpClient myClient = new HttpClient();
myClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", apiKey);
myClient.DefaultRequestHeaders.Add("Accept", "application/json");

var response = myClient.PostAsync(new Uri(url + "d9355f04-9bb2-4219-b9fa-734f2650a939/clone"), jsonContent).Result;
Debug.WriteLine(response.Content.ReadAsStringAsync().Result);

Running Your Code

Go ahead and run your code. You should be able to see your newly created template in your eSignLive account.

results template

There you go. You have successfully created a template from scratch and from a previously created package.

If you have questions regarding this blog or anything else concerning integrating eSignLive into your application, visit the developer community forums: https://developer.esignlive.com. That's it from me. Thank you for reading! If you found this post helpful, please share it on Facebook, Twitter, or LinkedIn. You can find all of my previous blogs by going to my author page.

Haris Haidary Junior

Technical Product Evangelist

LinkedIn | Twitter