Search code examples
c#docusignapidocusigncompositetmplts

How can I include multiple templates in an envelope?


I am unable to figure out how to send multiple populated DocuSign templates within an envelope using the DocuSign C# SDK. I am able to populate and send a single template within an envelope but am unable to do so with multiple templates.

This is a snippet of the code I use that successfully populates and sends the template:

var rolesList = new List<TemplateRole>();

var tRole = new TemplateRole   
{
    Tabs = new Tabs(),
    RoleName = "rolename"
};

tRole.Name = "joe smith";
tRole.Email = "email-address";

var tabData = new List<Text>();

// Populate the tabData 

tRole.Tabs.TextTabs = tabData;
rolesList.Add(tRole);

var envDef = new EnvelopeDefinition
{
    EmailSubject = "Pleae sign this",
    TemplateRoles = rolesList,
    TemplateId = "first template id",
    Status = "sent",
};

var envelopesApi = new EnvelopesApi();

await envelopesApi.CreateEnvelopeAsync("accountId", envDef);

For multiple templates, I assume I needed to use a CompositeTemplate so I tried this code:

var envDef = new EnvelopeDefinition
{
    EmailSubject = subject,
    TemplateRoles = roles,
    Status = send?  "sent" : "created",
};

var compositeTemplate = new CompositeTemplate()
{
    ServerTemplates = new List<ServerTemplate>(),
    CompositeTemplateId = Guid.NewGuid().ToString(),
};


envDef.CompositeTemplates = new List<CompositeTemplate>();
envDef.CompositeTemplates.Add(compositeTemplate);

ServerTemplate first = new ServerTemplate()
{
    TemplateId = "first template id",
    Sequence = "1"
};

ServerTemplate second = new ServerTemplate()
{
    TemplateId = "second template id",
    Sequence = "2",

};

compositeTemplate.ServerTemplates.Add(first);
compositeTemplate.ServerTemplates.Add(second);

var envelopesApi = new EnvelopesApi();

await envelopesApi.CreateEnvelopeAsync("accountId", envDef);

However, with this code the CreateEnvelopeAsync call throws the following Exception: DocuSign.eSign.Client.ApiException: Error calling CreateEnvelope: { "errorCode": "ENVELOPE_IS_INCOMPLETE", "message": "The Envelope is not Complete. A Complete Envelope Requires Documents, Recipients, Tabs, and a Subject Line." }

I'm obviously not configuring the CompositeTemplate correctly (most likely not populating the Documents) but I can't seem to find an example on how to correctly populate it.


Solution

  • Since your two server templates are separate from each other, I believe that you want to create an array of two composite templates. Each composite template item will contain one server template.

    See line 93 and below of this example. That example sends an envelope with

    1. A template from DocuSign (a "server template")
    2. A locally created HTML source document.

    In your case, repeat how item 1 is added instead of existing item 2.

    The key is two different composite template items, not two server templates within one composite template item.