Skip to content

Commit 025f649

Browse files
authored
Update eg045DeleteRestoreEnvelope.sh
Modified to accommodate the content model code examples and restrict examples only with the defined endpoints. Ideal workflow: 1. User select option 44. 2.User selects the default envelope id created using Step 2 or enter his own envelope id. 3. Envelope gets deleted and asks for user confirmation to check in the UI. 4. User enters the name of the destination folder where the restored envelope to be moved. 5. Moves the envelope to the destination folder. The new code snippet are introduced to follow the content model such as 1. Defining the headers. 2. Creating a requesting body And used Folders Endpoint (as suggested by Sigma team). Signed-off-by: Balaji Jayaraman <[email protected]>
1 parent a90a4f4 commit 025f649

File tree

1 file changed

+63
-25
lines changed

1 file changed

+63
-25
lines changed

examples/eSignature/eg045DeleteRestoreEnvelope.sh

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
# Delete and Restore an Envelope
1+
# Delete and Undelete an Envelope
22
#
33
# This script performs two sequential operations on a Docusign envelope:
44
# 1. Deletes the envelope by moving it to the Recycle Bin.
5-
# 2. Pauses for user confirmation.
6-
# 3. Restores the envelope from the Recycle Bin to the Sent Items folder.
5+
# 2. Pause for User Confirmation and Get the Destination Folder Name from User.
6+
# 3. Find the user-specified folder. If not available, use default folder.
7+
# 4. Undeletes the envelope from the Recycle Bin to a user-specified or default folder.
78
#
89

910
# Check that we're in a bash shell
@@ -21,6 +22,13 @@ account_id=$(cat config/API_ACCOUNT_ID)
2122

2223
base_path="https://demo.docusign.net/restapi"
2324

25+
# Construct your API headers
26+
#ds-snippet-start:eSign45Step2
27+
declare -a Headers=('--header' "Authorization: Bearer ${ACCESS_TOKEN}"
28+
'--header' "Accept: application/json"
29+
'--header' "Content-Type: application/json")
30+
#ds-snippet-end:eSign45Step2
31+
2432
# Check that we have an envelope id
2533
if [ ! -f config/ENVELOPE_ID ]; then
2634
echo ""
@@ -31,9 +39,8 @@ fi
3139
envelope_id=`cat config/ENVELOPE_ID`
3240

3341
# Get Envelope ID from user or config file
34-
3542
echo ""
36-
echo "Select the envelope ID to use for the delete and restore operations."
43+
echo "Select the envelope ID to use for the delete and undelete operations."
3744
echo ""
3845
if [ -f config/ENVELOPE_ID ]; then
3946
envelope_id_from_file=$(cat config/ENVELOPE_ID)
@@ -63,50 +70,81 @@ echo "Deleting the Envelope with ID: ${envelope_id}"
6370
echo "Sending PUT request to Docusign..."
6471
echo "Results:"
6572
echo ""
66-
67-
#ds-snippet-start:eSign45Step2
68-
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
69-
--header "Content-Type: application/json" \
70-
--request PUT "${base_path}/v2.1/accounts/${account_id}/folders/recyclebin" \
71-
--data-raw '{
72-
"envelopeIds": [
73-
"'${envelope_id}'"
74-
]
75-
}'
76-
#ds-snippet-end:eSign45Step2
73+
# Create the request body for deleting the envelope
74+
request_body=$(mktemp)
75+
#ds-snippet-start:eSign45Step3
76+
printf '{
77+
"envelopeIds": [
78+
"%s"
79+
]
80+
}' "${envelope_id}" > $request_body
81+
#ds-snippet-end:eSign45Step3
82+
83+
#ds-snippet-start:eSign45Step4
84+
curl --request PUT "${base_path}/v2.1/accounts/${account_id}/folders/recyclebin" \
85+
"${Headers[@]}" \
86+
--data-binary @${request_body}
87+
#ds-snippet-end:eSign45Step4
7788

7889
echo ""
7990
echo ""
8091
echo "The deleted envelope is now in your Docusign Recycle Bin."
8192
echo "You can check your web app to confirm the deletion."
8293

8394

84-
# PART 2: Pause for User Confirmation
95+
# PART 2: Pause for User Confirmation and Get the Destination Folder Name from User
8596

8697
echo ""
8798
read -p "Press Enter to proceed with restoring the envelope from the Recycle Bin..."
8899

89-
# PART 3: Restore the Envelope
100+
# Prompt for the destination folder name and handle spaces
101+
read -p "Please enter the name of the folder to undelete the envelope to (e.g., 'Sent Items') or press Enter to use the default: " destination_folder_name
102+
# Set default folder if none is provided
103+
if [ -z "$destination_folder_name" ]; then
104+
destination_folder_name="Sent Items"
105+
echo "The undeleted item will be moved to the Sent Items folder."
106+
fi
107+
108+
# PART 3: Find the Folder ID
109+
110+
echo "Searching for folder with name: '${destination_folder_name}'..."
111+
112+
# Store the API response in a variable
113+
#ds-snippet-start:eSign45Step5
114+
RESPONSE=$(curl --request GET \
115+
"${Headers[@]}" \
116+
"${base_path}/v2.1/accounts/${account_id}/folders")
117+
118+
# Find the specific folder entered and extract its folderId
119+
120+
folder_id=$(echo "${RESPONSE}" | grep -oi "\"name\":\"${destination_folder_name}\",\"type\":\"[^\"]*\",\"owner\":{[^\}]*},\"folderId\":\"[^\"]*\"" | sed 's/.*"folderId":"//' | sed 's/"$//')
121+
#ds-snippet-end:eSign45Step5
122+
if [ -z "$folder_id" ]; then
123+
echo "ERROR: Could not find a folder with the name '${destination_folder_name}'. Please check the spelling."
124+
fi
125+
126+
echo "Found folder ID: ${folder_id} for folder name: '${destination_folder_name}'"
127+
128+
# PART 4: Undelete the Envelope
90129

91130
echo ""
92-
echo "Restoring the Envelope from Recycle Bin to the Sent Items folder."
131+
echo "Restoring the Envelope from Recycle Bin to the '${destination_folder_name}' folder."
93132
echo "Sending PUT request to Docusign..."
94133
echo "Results:"
95134
echo ""
96135

97-
#ds-snippet-start:eSign45Step3
98-
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
99-
--header "Content-Type: application/json" \
100-
--request PUT "${base_path}/v2.1/accounts/${account_id}/folders/sentitems" \
136+
#ds-snippet-start:eSign45Step6
137+
curl --request PUT "${base_path}/v2.1/accounts/${account_id}/folders/${folder_id}" \
138+
"${Headers[@]}" \
101139
--data-raw '{
102140
"envelopeIds": [
103141
"'${envelope_id}'"
104142
],
105143
"fromFolderId": "recyclebin"
106144
}'
107-
#ds-snippet-end:eSign45Step3
145+
#ds-snippet-end:eSign45Step6
108146

109147
echo ""
110148
echo ""
111-
echo "The envelope has been restored and is now in your Docusign Sent Items folder."
149+
echo "The envelope has been undeleted and is now in your '${destination_folder_name}' folder."
112150
echo ""

0 commit comments

Comments
 (0)