|
| 1 | +// <copyright file="CreateRemoteInstanceService.cs" company="DocuSign"> |
| 2 | +// Copyright (c) DocuSign. All rights reserved. |
| 3 | +// </copyright> |
| 4 | + |
| 5 | +namespace DocuSign.WebForms.Examples |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.IO; |
| 10 | + using DocuSign.eSign.Api; |
| 11 | + using DocuSign.eSign.Client; |
| 12 | + using DocuSign.eSign.Model; |
| 13 | + using DocuSign.WebForms.Api; |
| 14 | + using DocuSign.WebForms.Model; |
| 15 | + |
| 16 | + public static class CreateRemoteInstanceService |
| 17 | + { |
| 18 | + public static WebFormSummaryList GetFormsByName(string basePath, string accessToken, string accountId, string templateName) |
| 19 | + { |
| 20 | + var client = PrepareWebFormsClient(accessToken, basePath); |
| 21 | + FormManagementApi formManagementApi = new FormManagementApi(client); |
| 22 | + |
| 23 | + FormManagementApi.ListFormsOptions listFormsOptions = new FormManagementApi.ListFormsOptions |
| 24 | + { |
| 25 | + search = templateName, |
| 26 | + }; |
| 27 | + return formManagementApi.ListForms(accountId, listFormsOptions); |
| 28 | + } |
| 29 | + |
| 30 | + public static void AddTemplateIdToForm(string fileLocation, string templateId) |
| 31 | + { |
| 32 | + string targetString = "template-id"; |
| 33 | + string fileContent = File.ReadAllText(fileLocation); |
| 34 | + string modifiedContent = fileContent.Replace(targetString, templateId); |
| 35 | + |
| 36 | + File.WriteAllText(fileLocation, modifiedContent); |
| 37 | + } |
| 38 | + |
| 39 | + public static WebFormInstance CreateInstance( |
| 40 | + string basePath, |
| 41 | + string accessToken, |
| 42 | + string accountId, |
| 43 | + string formId, |
| 44 | + string signerEmail, |
| 45 | + string signerName) |
| 46 | + { |
| 47 | + var client = PrepareWebFormsClient(accessToken, basePath); |
| 48 | + |
| 49 | + var formValues = new WebFormValues |
| 50 | + { |
| 51 | + { "PhoneNumber", "555-555-5555" }, |
| 52 | + { "Yes", new[] { "Yes" } }, |
| 53 | + { "Company", "Tally" }, |
| 54 | + { "JobTitle", "Programmer Writer" }, |
| 55 | + }; |
| 56 | + |
| 57 | + var requestBody = new CreateInstanceRequestBody() |
| 58 | + { |
| 59 | + SendOption = SendOption.Now, |
| 60 | + FormValues = formValues, |
| 61 | + Recipients = new List<CreateInstanceRequestBodyRecipients> |
| 62 | + { |
| 63 | + new CreateInstanceRequestBodyRecipients |
| 64 | + { |
| 65 | + Email = signerEmail, |
| 66 | + Name = signerName, |
| 67 | + RoleName = "signer", |
| 68 | + }, |
| 69 | + }, |
| 70 | + }; |
| 71 | + |
| 72 | + FormInstanceManagementApi formManagementApi = new FormInstanceManagementApi(client); |
| 73 | + return formManagementApi.CreateInstance(accountId, formId, requestBody); |
| 74 | + } |
| 75 | + |
| 76 | + public static List<EnvelopeTemplate> GetTemplatesByName( |
| 77 | + string basePath, |
| 78 | + string accessToken, |
| 79 | + string accountId, |
| 80 | + string templateName) |
| 81 | + { |
| 82 | + var client = PrepareESignClient(accessToken, basePath); |
| 83 | + var templatesApi = new TemplatesApi(client); |
| 84 | + |
| 85 | + var options = new TemplatesApi.ListTemplatesOptions |
| 86 | + { |
| 87 | + searchText = templateName, |
| 88 | + }; |
| 89 | + |
| 90 | + EnvelopeTemplateResults templates = templatesApi.ListTemplates(accountId, options); |
| 91 | + |
| 92 | + return templates.EnvelopeTemplates; |
| 93 | + } |
| 94 | + |
| 95 | + public static TemplateSummary CreateTemplate( |
| 96 | + string basePath, |
| 97 | + string accessToken, |
| 98 | + string accountId, |
| 99 | + string documentPdf, |
| 100 | + string templateName) |
| 101 | + { |
| 102 | + var client = PrepareESignClient(accessToken, basePath); |
| 103 | + TemplatesApi templatesApi = new TemplatesApi(client); |
| 104 | + |
| 105 | + EnvelopeTemplate envelopeTemplate = PrepareEnvelopeTemplate(templateName, documentPdf); |
| 106 | + |
| 107 | + return templatesApi.CreateTemplate(accountId, envelopeTemplate); |
| 108 | + } |
| 109 | + |
| 110 | + public static EnvelopeTemplate PrepareEnvelopeTemplate(string resultsTemplateName, string documentPdf) |
| 111 | + { |
| 112 | + Document document = new Document() |
| 113 | + { |
| 114 | + DocumentBase64 = Convert.ToBase64String(File.ReadAllBytes(documentPdf)), |
| 115 | + Name = "World_Wide_Web_Form", |
| 116 | + FileExtension = "pdf", |
| 117 | + DocumentId = "1", |
| 118 | + }; |
| 119 | + |
| 120 | + Signer signer = new Signer() |
| 121 | + { |
| 122 | + RoleName = "signer", |
| 123 | + RecipientId = "1", |
| 124 | + RoutingOrder = "1", |
| 125 | + }; |
| 126 | + |
| 127 | + Tabs signerTabs = new Tabs() |
| 128 | + { |
| 129 | + CheckboxTabs = new List<Checkbox> |
| 130 | + { |
| 131 | + new Checkbox() |
| 132 | + { |
| 133 | + DocumentId = "1", |
| 134 | + TabLabel = "Yes", |
| 135 | + AnchorString = "/SMS/", |
| 136 | + AnchorUnits = "pixels", |
| 137 | + AnchorXOffset = "0", |
| 138 | + AnchorYOffset = "0", |
| 139 | + }, |
| 140 | + }, |
| 141 | + SignHereTabs = new List<SignHere> |
| 142 | + { |
| 143 | + new SignHere() |
| 144 | + { |
| 145 | + DocumentId = "1", |
| 146 | + TabLabel = "Signature", |
| 147 | + AnchorString = "/SignHere/", |
| 148 | + AnchorUnits = "pixels", |
| 149 | + AnchorXOffset = "20", |
| 150 | + AnchorYOffset = "10", |
| 151 | + }, |
| 152 | + }, |
| 153 | + TextTabs = new List<Text> |
| 154 | + { |
| 155 | + new Text() |
| 156 | + { |
| 157 | + DocumentId = "1", |
| 158 | + TabLabel = "FullName", |
| 159 | + AnchorString = "/FullName/", |
| 160 | + AnchorUnits = "pixels", |
| 161 | + AnchorXOffset = "0", |
| 162 | + AnchorYOffset = "0", |
| 163 | + }, |
| 164 | + new Text() |
| 165 | + { |
| 166 | + DocumentId = "1", |
| 167 | + TabLabel = "PhoneNumber", |
| 168 | + AnchorString = "/PhoneNumber/", |
| 169 | + AnchorUnits = "pixels", |
| 170 | + AnchorXOffset = "0", |
| 171 | + AnchorYOffset = "0", |
| 172 | + }, |
| 173 | + new Text() |
| 174 | + { |
| 175 | + DocumentId = "1", |
| 176 | + TabLabel = "Company", |
| 177 | + AnchorString = "/Company/", |
| 178 | + AnchorUnits = "pixels", |
| 179 | + AnchorXOffset = "0", |
| 180 | + AnchorYOffset = "0", |
| 181 | + }, |
| 182 | + new Text() |
| 183 | + { |
| 184 | + DocumentId = "1", |
| 185 | + TabLabel = "JobTitle", |
| 186 | + AnchorString = "/Title/", |
| 187 | + AnchorUnits = "pixels", |
| 188 | + AnchorXOffset = "0", |
| 189 | + AnchorYOffset = "0", |
| 190 | + }, |
| 191 | + }, |
| 192 | + DateSignedTabs = new List<DateSigned> |
| 193 | + { |
| 194 | + new DateSigned() |
| 195 | + { |
| 196 | + DocumentId = "1", |
| 197 | + TabLabel = "DateSigned", |
| 198 | + AnchorString = "/Date/", |
| 199 | + AnchorUnits = "pixels", |
| 200 | + AnchorXOffset = "0", |
| 201 | + AnchorYOffset = "0", |
| 202 | + }, |
| 203 | + }, |
| 204 | + }; |
| 205 | + |
| 206 | + signer.Tabs = signerTabs; |
| 207 | + |
| 208 | + Recipients recipients = new Recipients(); |
| 209 | + recipients.Signers = new List<Signer> { signer }; |
| 210 | + |
| 211 | + return new EnvelopeTemplate() |
| 212 | + { |
| 213 | + Description = "Example template created via the eSignature API", |
| 214 | + Name = resultsTemplateName, |
| 215 | + Shared = "false", |
| 216 | + Documents = new List<Document> { document }, |
| 217 | + EmailSubject = "Please sign this document", |
| 218 | + Recipients = recipients, |
| 219 | + Status = "created", |
| 220 | + }; |
| 221 | + } |
| 222 | + |
| 223 | + private static Client.DocuSignClient PrepareWebFormsClient(string accessToken, string basePath) |
| 224 | + { |
| 225 | + var client = new Client.DocuSignClient(basePath); |
| 226 | + client.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken); |
| 227 | + return client; |
| 228 | + } |
| 229 | + |
| 230 | + private static DocuSignClient PrepareESignClient(string accessToken, string basePath) |
| 231 | + { |
| 232 | + var client = new DocuSignClient(basePath); |
| 233 | + client.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken); |
| 234 | + return client; |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments