atbebtg
atbebtg

Reputation: 4083

Adding document to docusign envelope always return false

I'm using the following code to try to add a pdf document to an existing template with multiple documents using the AddDocument function of the c# API but the result is always false. The template is succesfully sent with all the preset documents sent correctly. How do I correctly add the pdf document? I have to add the pdf document using code because this particular document is different every time we send the template. I have tested GetIPS function and it returned the byte[] for the pdf document so I know that's not the issue.

Here are my codes

            byte[] ips = GetIPS("");
            RestSettings.Instance.DocuSignAddress = "https://demo.docusign.net";
            RestSettings.Instance.WebServiceUrl = RestSettings.Instance.DocuSignAddress + "/restapi/v2";
            RestSettings.Instance.IntegratorKey = integratorKey;

            DocuSign.Integrations.Client.Account account = new DocuSign.Integrations.Client.Account();
            account.Email = username;
            account.Password = password;
            var loginResult = account.Login();


            Template template = new Template();

            template.TemplateId = templateId;
            template.Login = account;


            template.EmailSubject = emailSubject;
            template.EmailBlurb = emailMessage;
            var documents = template.GetDocuments();

            TemplateRole tr = new TemplateRole();

            var roles = new List<TemplateRole>();

            //Handle Primary Client
            roles.Add(new TemplateRole
            {
                roleName = "Primary Client",
                name = primaryClientName,
                email = primaryClientEmail,
                tabs = new RoleTabs
                {
                    textTabs = new RoleTextTab[] {
                        new RoleTextTab {
                            tabLabel = "FeeEffectiveDate",
                            value = effectiveDate
                        },
                        new RoleTextTab {
                            tabLabel = "FeePercentage",
                            value = fee
                        }
                    }
                },
            });

            if (secondaryClientName.Trim().Length != 0)
            {
                roles.Add(new TemplateRole
                {
                    roleName = "Secondary Client",
                    name = secondaryClientName,
                    email = secondaryClientEmail,
                });
            }


            roles.Add(new TemplateRole
            {
                roleName = "President",
                name = presidentName,
                email = presidentEmail,
            });

            roles.Add(new TemplateRole
            {
                roleName = "Css",
                name = cssName,
                email = cssEmail,
            });


            template.TemplateRoles = roles.ToArray<TemplateRole>();
            template.Status = "sent"; 

            //The following code always return false
            bool status = template.AddDocument(ips, "IPS.pdf", 1);
            var result = template.Create();

Upvotes: 0

Views: 331

Answers (1)

Almog G.
Almog G.

Reputation: 817

In order to use the AddDocument function, an envelope must be in draft state (as you can also see in the remarks for this function in the source code). Therefore, in your case, you must first create a draft envelope (by changing the envelope status to "created"), then invoke the AddDocument function, and finally update the envelope status to "sent" to send the envelope.

For example:

.
.
.
template.Status = "created";
var result = template.Create();

bool status = template.AddDocument(ips, "IPS.pdf", 2);

template.Status = "sent";
result = template.UpdateStatus();

Note that the document index is the document ID, and is must be different from the IDs of the existing documents in your template. Otherwise, an existing document that has the same ID number, will be replaced by the new document.

Upvotes: 1

Related Questions