@@ -8,57 +8,102 @@ namespace DocuSign.CodeExamples.Examples
88 using System . Collections . Generic ;
99 using System . Linq ;
1010 using System . Net . Http ;
11- using System . Net . Http . Headers ;
12- using System . Text ;
13- using System . Text . Json ;
1411 using System . Threading . Tasks ;
15- using DocuSign . CodeExamples . ConnectedFields . Models ;
1612 using DocuSign . eSign . Api ;
1713 using DocuSign . eSign . Client ;
1814 using DocuSign . eSign . Model ;
19- using static System . Net . Mime . MediaTypeNames ;
15+ using Newtonsoft . Json ;
16+ using Newtonsoft . Json . Linq ;
2017
2118 public static class SetConnectedFields
2219 {
2320 private static readonly HttpClient Client = new HttpClient ( ) ;
2421
25- public static async Task < ExtensionApps > GetConnectedFieldsAsync ( string accessToken , string accountId )
22+ public static async Task < object > GetConnectedFieldsTabGroupsAsync ( string accountId , string accessToken )
2623 {
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";
2925
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
3132 {
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 ( ) ;
3535
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 ) ;
3838
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 } ") ;
4044 }
4145 }
4246
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 )
4448 {
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 ) ;
4685 var docuSignClient = new DocuSignClient ( basePath ) ;
4786 docuSignClient . Configuration . DefaultHeader . Add ( "Authorization" , "Bearer " + accessToken ) ;
4887
4988 EnvelopesApi envelopesApi = new EnvelopesApi ( docuSignClient ) ;
50- EnvelopeSummary results = envelopesApi . CreateEnvelope ( accountId , env ) ;
89+ EnvelopeSummary results = envelopesApi . CreateEnvelope ( accountId , envelopeDefinition ) ;
5190 return results . EnvelopeId ;
5291 }
5392
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 )
5598 {
99+ var appId = selectedApp [ "appId" ] ? . ToString ( ) ?? string . Empty ;
100+ JArray tabLabels = ( JArray ) selectedApp [ "tabs" ] ;
101+
56102 EnvelopeDefinition envelopeDefinition = new EnvelopeDefinition ( ) ;
57103 envelopeDefinition . EmailSubject = "Please sign this document set" ;
58104 envelopeDefinition . Status = "sent" ;
59105
60106 string docPdfBytes = Convert . ToBase64String ( System . IO . File . ReadAllBytes ( docPdf ) ) ;
61-
62107 Document document = new Document
63108 {
64109 DocumentBase64 = docPdfBytes ,
@@ -75,7 +120,7 @@ public static EnvelopeDefinition MakeEnvelope(string signerEmail, string signerN
75120 Name = signerName ,
76121 RecipientId = "1" ,
77122 RoutingOrder = "1" ,
78- Tabs = new eSign . Model . Tabs
123+ Tabs = new Tabs
79124 {
80125 SignHereTabs = new List < SignHere >
81126 {
@@ -87,36 +132,81 @@ public static EnvelopeDefinition MakeEnvelope(string signerEmail, string signerN
87132 AnchorXOffset = "20" ,
88133 } ,
89134 } ,
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 >
93191 {
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+ } ,
115197 } ,
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 ,
117208 } ,
118209 } ;
119-
120210 return envelopeDefinition ;
121211 }
122212 }
0 commit comments