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