|
| 1 | +// <copyright file="SetConnectedFields.cs" company="DocuSign"> |
| 2 | +// Copyright (c) DocuSign. All rights reserved. |
| 3 | +// </copyright> |
| 4 | + |
| 5 | +namespace DocuSign.CodeExamples.Examples |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.Linq; |
| 10 | + using System.Net.Http; |
| 11 | + using System.Threading.Tasks; |
| 12 | + using DocuSign.eSign.Api; |
| 13 | + using DocuSign.eSign.Client; |
| 14 | + using DocuSign.eSign.Model; |
| 15 | + using Newtonsoft.Json; |
| 16 | + using Newtonsoft.Json.Linq; |
| 17 | + |
| 18 | + public static class SetConnectedFields |
| 19 | + { |
| 20 | + private static readonly HttpClient Client = new HttpClient(); |
| 21 | + |
| 22 | + public static async Task<object> GetConnectedFieldsTabGroupsAsync(string accountId, string accessToken) |
| 23 | + { |
| 24 | + //ds-snippet-start:ConnectedFields1Step3 |
| 25 | + var url = $"https://api-d.docusign.com/v1/accounts/{accountId}/connected-fields/tab-groups"; |
| 26 | + |
| 27 | + var requestMessage = new HttpRequestMessage(HttpMethod.Get, url); |
| 28 | + //ds-snippet-end:ConnectedFields1Step3 |
| 29 | + |
| 30 | + //ds-snippet-start:ConnectedFields1Step2 |
| 31 | + requestMessage.Headers.Add("Authorization", $"Bearer {accessToken}"); |
| 32 | + requestMessage.Headers.Add("Accept", "application/json"); |
| 33 | + //ds-snippet-end:ConnectedFields1Step2 |
| 34 | + |
| 35 | + try |
| 36 | + { |
| 37 | + //ds-snippet-start:ConnectedFields1Step3 |
| 38 | + var response = await Client.SendAsync(requestMessage); |
| 39 | + response.EnsureSuccessStatusCode(); |
| 40 | + |
| 41 | + var body = await response.Content.ReadAsStringAsync(); |
| 42 | + var data = JsonConvert.DeserializeObject<object>(body); |
| 43 | + |
| 44 | + return data; |
| 45 | + //ds-snippet-end:ConnectedFields1Step3 |
| 46 | + } |
| 47 | + catch (HttpRequestException e) |
| 48 | + { |
| 49 | + throw new Exception($"DocuSign API Request failed: {e.Message}"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static JArray FilterData(JArray data) |
| 54 | + { |
| 55 | + //ds-snippet-start:ConnectedFields1Step4 |
| 56 | + var filteredData = data.Where(item => |
| 57 | + { |
| 58 | + var tabs = item["tabs"] as JArray; |
| 59 | + if (tabs == null) |
| 60 | + { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + foreach (var tab in tabs) |
| 65 | + { |
| 66 | + var extensionData = tab["extensionData"]; |
| 67 | + var tabLabel = tab["tabLabel"]?.ToString(); |
| 68 | + |
| 69 | + if ((extensionData != null && extensionData["actionContract"]?.ToString().Contains("Verify") == true) || |
| 70 | + (tabLabel != null && tabLabel.Contains("connecteddata"))) |
| 71 | + { |
| 72 | + return true; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return false; |
| 77 | + }).ToList(); |
| 78 | + //ds-snippet-end:ConnectedFields1Step4 |
| 79 | + |
| 80 | + return new JArray(filteredData); |
| 81 | + } |
| 82 | + |
| 83 | + public static string SendEnvelopeViaEmail( |
| 84 | + string basePath, |
| 85 | + string accessToken, |
| 86 | + string accountId, |
| 87 | + string signerEmail, |
| 88 | + string signerName, |
| 89 | + string docPdf, |
| 90 | + JObject selectedApp) |
| 91 | + { |
| 92 | + //ds-snippet-start:ConnectedFields1Step6 |
| 93 | + EnvelopeDefinition envelopeDefinition = MakeEnvelope(signerEmail, signerName, docPdf, selectedApp); |
| 94 | + var docuSignClient = new DocuSignClient(basePath); |
| 95 | + docuSignClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken); |
| 96 | + |
| 97 | + EnvelopesApi envelopesApi = new EnvelopesApi(docuSignClient); |
| 98 | + EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, envelopeDefinition); |
| 99 | + //ds-snippet-end:ConnectedFields1Step6 |
| 100 | + return results.EnvelopeId; |
| 101 | + } |
| 102 | + |
| 103 | + //ds-snippet-start:ConnectedFields1Step5 |
| 104 | + public static EnvelopeDefinition MakeEnvelope( |
| 105 | + string signerEmail, |
| 106 | + string signerName, |
| 107 | + string docPdf, |
| 108 | + JObject selectedApp) |
| 109 | + { |
| 110 | + var appId = selectedApp["appId"]?.ToString() ?? string.Empty; |
| 111 | + JArray tabLabels = (JArray)selectedApp["tabs"]; |
| 112 | + |
| 113 | + EnvelopeDefinition envelopeDefinition = new EnvelopeDefinition(); |
| 114 | + envelopeDefinition.EmailSubject = "Please sign this document set"; |
| 115 | + envelopeDefinition.Status = "sent"; |
| 116 | + |
| 117 | + string docPdfBytes = Convert.ToBase64String(System.IO.File.ReadAllBytes(docPdf)); |
| 118 | + Document document = new Document |
| 119 | + { |
| 120 | + DocumentBase64 = docPdfBytes, |
| 121 | + Name = "Lorem Ipsum", |
| 122 | + FileExtension = "pdf", |
| 123 | + DocumentId = "1", |
| 124 | + }; |
| 125 | + |
| 126 | + envelopeDefinition.Documents = new List<Document> { document }; |
| 127 | + |
| 128 | + Signer signer = new Signer |
| 129 | + { |
| 130 | + Email = signerEmail, |
| 131 | + Name = signerName, |
| 132 | + RecipientId = "1", |
| 133 | + RoutingOrder = "1", |
| 134 | + Tabs = new Tabs |
| 135 | + { |
| 136 | + SignHereTabs = new List<SignHere> |
| 137 | + { |
| 138 | + new SignHere |
| 139 | + { |
| 140 | + AnchorString = "/sn1/", |
| 141 | + AnchorUnits = "pixels", |
| 142 | + AnchorYOffset = "10", |
| 143 | + AnchorXOffset = "20", |
| 144 | + }, |
| 145 | + }, |
| 146 | + TextTabs = new List<Text>(), |
| 147 | + }, |
| 148 | + }; |
| 149 | + |
| 150 | + foreach (var tab in tabLabels) |
| 151 | + { |
| 152 | + var connectionKey = tab["extensionData"]["connectionInstances"] != null ? |
| 153 | + tab["extensionData"]["connectionInstances"][0]?["connectionKey"]?.ToString() : string.Empty; |
| 154 | + var connectionValue = tab["extensionData"]["connectionInstances"] != null ? |
| 155 | + tab["extensionData"]["connectionInstances"][0]?["connectionValue"]?.ToString() : string.Empty; |
| 156 | + var extensionGroupId = tab["extensionData"]["extensionGroupId"]?.ToString() ?? string.Empty; |
| 157 | + var publisherName = tab["extensionData"]["publisherName"]?.ToString() ?? string.Empty; |
| 158 | + var applicationName = tab["extensionData"]["applicationName"]?.ToString() ?? string.Empty; |
| 159 | + var actionName = tab["extensionData"]["actionName"]?.ToString() ?? string.Empty; |
| 160 | + var actionInputKey = tab["extensionData"]["actionInputKey"]?.ToString() ?? string.Empty; |
| 161 | + var actionContract = tab["extensionData"]["actionContract"]?.ToString() ?? string.Empty; |
| 162 | + var extensionName = tab["extensionData"]["extensionName"]?.ToString() ?? string.Empty; |
| 163 | + var extensionContract = tab["extensionData"]["extensionContract"]?.ToString() ?? string.Empty; |
| 164 | + var requiredForExtension = tab["extensionData"]["requiredForExtension"]?.ToString() ?? string.Empty; |
| 165 | + |
| 166 | + var text = new Text |
| 167 | + { |
| 168 | + RequireInitialOnSharedChange = "false", |
| 169 | + RequireAll = "false", |
| 170 | + Name = applicationName, |
| 171 | + Required = "false", |
| 172 | + Locked = "false", |
| 173 | + DisableAutoSize = "false", |
| 174 | + MaxLength = "4000", |
| 175 | + TabLabel = tab["tabLabel"].ToString(), |
| 176 | + Font = "lucidaconsole", |
| 177 | + FontColor = "black", |
| 178 | + FontSize = "size9", |
| 179 | + DocumentId = "1", |
| 180 | + RecipientId = "1", |
| 181 | + PageNumber = "1", |
| 182 | + XPosition = "273", |
| 183 | + YPosition = "191", |
| 184 | + Width = "84", |
| 185 | + Height = "22", |
| 186 | + TemplateRequired = "false", |
| 187 | + TabType = "text", |
| 188 | + ExtensionData = new ExtensionData |
| 189 | + { |
| 190 | + ExtensionGroupId = extensionGroupId, |
| 191 | + PublisherName = publisherName, |
| 192 | + ApplicationId = appId, |
| 193 | + ApplicationName = applicationName, |
| 194 | + ActionName = actionName, |
| 195 | + ActionContract = actionContract, |
| 196 | + ExtensionName = extensionName, |
| 197 | + ExtensionContract = extensionContract, |
| 198 | + RequiredForExtension = requiredForExtension, |
| 199 | + ActionInputKey = actionInputKey, |
| 200 | + ExtensionPolicy = "MustVerifyToSign", |
| 201 | + ConnectionInstances = new List<ConnectionInstance> |
| 202 | + { |
| 203 | + new ConnectionInstance |
| 204 | + { |
| 205 | + ConnectionKey = connectionKey, |
| 206 | + ConnectionValue = connectionValue, |
| 207 | + }, |
| 208 | + }, |
| 209 | + }, |
| 210 | + }; |
| 211 | + signer.Tabs.TextTabs.Add(text); |
| 212 | + } |
| 213 | + |
| 214 | + envelopeDefinition.Recipients = new Recipients |
| 215 | + { |
| 216 | + Signers = new List<Signer> |
| 217 | + { |
| 218 | + signer, |
| 219 | + }, |
| 220 | + }; |
| 221 | + return envelopeDefinition; |
| 222 | + } |
| 223 | + |
| 224 | + //ds-snippet-end:ConnectedFields1Step5 |
| 225 | + } |
| 226 | +} |
0 commit comments