Skip to content

Commit 21f284d

Browse files
author
Balaji Jayaraman
committed
Added code snippets and codedepot markers for delete and undelete envelope howto
1 parent 9f20111 commit 21f284d

File tree

2 files changed

+158
-1
lines changed

2 files changed

+158
-1
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
$apiUri = "https://demo.docusign.net/restapi"
2+
3+
# Delete and Undelete an Envelope
4+
5+
# Get required environment variables from .\config\settings.json file
6+
$variables = Get-Content .\config\settings.json -Raw | ConvertFrom-Json
7+
8+
9+
# Obtain your OAuth token
10+
# Note: Substitute these values with your own
11+
$accessToken = Get-Content .\config\ds_access_token.txt
12+
13+
# Set up variables for full code example
14+
# Note: Substitute these values with your own
15+
$accountId = Get-Content .\config\API_ACCOUNT_ID
16+
17+
# temp files:
18+
$requestData = New-TemporaryFile
19+
$response = New-TemporaryFile
20+
21+
$recycle_bin_folder_id = "recyclebin"
22+
23+
Write-Output "Select the envelope ID to use for the delete and undelete operations."
24+
if (Test-Path .\config\ENVELOPE_ID) {
25+
$envelopeIdFromFile = Get-Content .\config\ENVELOPE_ID
26+
27+
$userSavedEnvelope = Read-Host "Use the envelope ID from 'config/ENVELOPE_ID' (${envelopeIdFromFile})? (y/n)"
28+
switch ($userSavedEnvelope.ToLower()) {
29+
"y" {
30+
$envelopeId = $envelopeIdFromFile
31+
}
32+
default {
33+
$envelopeId = Read-Host "Please enter the new envelope ID"
34+
}
35+
}
36+
} else {
37+
$envelopeId = Read-Host "No envelope ID found. Please enter the envelope ID"
38+
}
39+
40+
if (-not $envelopeId) {
41+
Write-Output "ERROR: No envelope ID was provided"
42+
exit 1
43+
}
44+
45+
Write-Output "Deleting the Envelope with ID: ${envelopeId}"
46+
Write-Output "Sending PUT request to Docusign..."
47+
Write-Output "Results:"
48+
49+
#ds-snippet-start:eSign45Step2
50+
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
51+
$headers.add("Authorization", "Bearer $accessToken")
52+
$headers.add("Content-Type", "application/json")
53+
#ds-snippet-end:eSign45Step2
54+
55+
# Concatenate the different parts of the request
56+
#ds-snippet-start:eSign45Step3
57+
@{
58+
envelopeIds = @("$envelopeId")
59+
} | ConvertTo-Json -Depth 32 > $requestData
60+
#ds-snippet-end:eSign45Step3
61+
62+
# Create and send the folders request
63+
#ds-snippet-start:eSign45Step4
64+
Invoke-RestMethod `
65+
-Uri "${apiUri}/v2.1/accounts/${accountId}/folders/${recycle_bin_folder_id}" `
66+
-Method 'PUT' `
67+
-Headers $headers `
68+
-InFile (Resolve-Path $requestData).Path `
69+
-OutFile $response `
70+
#ds-snippet-end:eSign45Step4
71+
72+
Write-Output "The deleted envelope is now in your Docusign Recycle Bin."
73+
Write-Output "You can check your web app to confirm the deletion."
74+
75+
Read-Host "Press Enter to proceed with undeleting the envelope from the Recycle Bin..."
76+
$destinationFolderName = Read-Host "Please enter the name of the folder to undelete the envelope to (e.g., 'Sent Items') or press Enter to use the default"
77+
78+
if (-not $destinationFolderName) {
79+
$destinationFolderName = "Sent Items"
80+
Write-Output "The undeleted item will be moved to the Sent Items folder"
81+
}
82+
83+
Write-Output "Searching for folder with name: '${destinationFolderName}'..."
84+
85+
#ds-snippet-start:eSign45Step5
86+
function Get-FolderIdByName {
87+
param (
88+
[object]$folders,
89+
[string]$targetName
90+
)
91+
92+
foreach ($folder in $folders) {
93+
# Check this folder
94+
if ($folder.name -eq $targetName) {
95+
return $folder.folderId
96+
}
97+
98+
# If this folder has subfolders, search inside them
99+
if ($folder.folders) {
100+
$result = Get-FolderIdByName -folders $folder.folders -targetName $targetName
101+
if ($result) {
102+
return $result
103+
}
104+
}
105+
}
106+
107+
return $null
108+
}
109+
110+
Invoke-RestMethod `
111+
-Uri "${apiUri}/v2.1/accounts/${accountId}/folders" `
112+
-Method 'GET' `
113+
-Headers $headers `
114+
-OutFile $response
115+
116+
$folders = $(Get-Content $response | ConvertFrom-Json).folders
117+
$folderId = Get-FolderIdByName -folders $folders -targetName $destinationFolderName
118+
#ds-snippet-end:eSign45Step5
119+
120+
if (-not $folderId) {
121+
Write-Output "ERROR: Could not find a folder with the name '${destinationFolderName}'. Please check the spelling."
122+
exit 1
123+
}
124+
125+
Write-Output "Found folder ID: ${folderId} for folder name: '${destinationFolderName}'"
126+
127+
Write-Output "Undeleting the Envelope from Recycle Bin to the '${destinationFolderName}' folder."
128+
Write-Output "Sending PUT request to Docusign..."
129+
Write-Output "Results:"
130+
131+
#ds-snippet-start:eSign45Step6
132+
@{
133+
envelopeIds = @("$envelopeId");
134+
fromFolderId = "$recycle_bin_folder_id"
135+
} | ConvertTo-Json -Depth 32 > $requestData
136+
137+
# Create and send the folders request
138+
Invoke-RestMethod `
139+
-Uri "${apiUri}/v2.1/accounts/${accountId}/folders/${folderId}" `
140+
-Method 'PUT' `
141+
-Headers $headers `
142+
-InFile (Resolve-Path $requestData).Path `
143+
-OutFile $response
144+
#ds-snippet-end:eSign45Step6
145+
146+
Write-Output "The envelope has been undeleted and is now in your '${destinationFolderName}' folder."
147+
148+
# cleanup
149+
Remove-Item $requestData
150+
Remove-Item $response
151+
152+
Write-Output "Done."

launcher.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ function startSignature {
501501
Document_Generation = 42;
502502
Shared_Access = 43;
503503
Focused_View = 44;
504-
Pick_An_API = 45;
504+
Delete_Restore_Envelope = 45;
505+
Pick_An_API = 46;
505506
}
506507

507508
$ApiExamplesView = $null;
@@ -550,6 +551,7 @@ function startSignature {
550551
Write-Output "$([int][ApiExamples]::Document_Generation)) Document_Generation"
551552
Write-Output "$([int][ApiExamples]::Shared_Access)) Shared_Access"
552553
Write-Output "$([int][ApiExamples]::Focused_View)) Focused_View"
554+
Write-Output "$([int][ApiExamples]::Delete_Restore_Envelope)) Delete_Restore_Envelope"
553555
Write-Output "$([int][ApiExamples]::Pick_An_API)) Pick_An_API"
554556
[int]$ApiExamplesView = Read-Host "Select the action"
555557
} while (-not [ApiExamples]::IsDefined([ApiExamples], $ApiExamplesView));
@@ -700,6 +702,9 @@ function startSignature {
700702
checkEmailAddresses
701703
Invoke-Script -Command "`".\examples\eSignature\eg044FocusedView.ps1`""
702704
}
705+
elseif ($ApiExamplesView -eq [ApiExamples]::Delete_Restore_Envelope) {
706+
Invoke-Script -Command "`".\examples\eSignature\eg045DeleteRestoreEnvelope.ps1`""
707+
}
703708
} until ($ApiExamplesView -eq [ApiExamples]::Pick_An_API)
704709
startLauncher
705710
}

0 commit comments

Comments
 (0)