-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwalkthrough.py
More file actions
364 lines (316 loc) · 13.8 KB
/
walkthrough.py
File metadata and controls
364 lines (316 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
"""
Walkthrough demonstrating core Dataverse SDK operations.
This example shows:
- Table creation with various column types including enums
- Single and multiple record CRUD operations
- Querying with filtering, paging, and SQL
- Picklist label-to-value conversion
- Column management
- Cleanup
Prerequisites:
- pip install PowerPlatform-Dataverse-Client
- pip install azure-identity
"""
import sys
import json
import time
from enum import IntEnum
from azure.identity import InteractiveBrowserCredential
from PowerPlatform.Dataverse.client import DataverseClient
from PowerPlatform.Dataverse.core.errors import MetadataError
import requests
# Simple logging helper
def log_call(description):
print(f"\n-> {description}")
# Define enum for priority picklist
class Priority(IntEnum):
LOW = 1
MEDIUM = 2
HIGH = 3
def backoff(op, *, delays=(0, 2, 5, 10, 20, 20)):
last = None
total_delay = 0
attempts = 0
for d in delays:
if d:
time.sleep(d)
total_delay += d
attempts += 1
try:
result = op()
if attempts > 1:
retry_count = attempts - 1
print(f" [INFO] Backoff succeeded after {retry_count} retry(s); waited {total_delay}s total.")
return result
except Exception as ex: # noqa: BLE001
last = ex
continue
if last:
if attempts:
retry_count = max(attempts - 1, 0)
print(f" [WARN] Backoff exhausted after {retry_count} retry(s); waited {total_delay}s total.")
raise last
def main():
print("=" * 80)
print("Dataverse SDK Walkthrough")
print("=" * 80)
# ============================================================================
# 1. SETUP & AUTHENTICATION
# ============================================================================
print("\n" + "=" * 80)
print("1. Setup & Authentication")
print("=" * 80)
base_url = input("Enter Dataverse org URL (e.g. https://yourorg.crm.dynamics.com): ").strip()
if not base_url:
print("No URL entered; exiting.")
sys.exit(1)
base_url = base_url.rstrip("/")
log_call("InteractiveBrowserCredential()")
credential = InteractiveBrowserCredential()
log_call(f"DataverseClient(base_url='{base_url}', credential=...)")
client = DataverseClient(base_url=base_url, credential=credential)
print(f"[OK] Connected to: {base_url}")
# ============================================================================
# 2. TABLE CREATION (METADATA)
# ============================================================================
print("\n" + "=" * 80)
print("2. Table Creation (Metadata)")
print("=" * 80)
table_name = "new_WalkthroughDemo"
log_call(f"client.tables.get('{table_name}')")
table_info = backoff(lambda: client.tables.get(table_name))
if table_info:
print(f"[OK] Table already exists: {table_info.get('table_schema_name')}")
print(f" Logical Name: {table_info.get('table_logical_name')}")
print(f" Entity Set: {table_info.get('entity_set_name')}")
else:
log_call(f"client.tables.create('{table_name}', columns={{...}})")
columns = {
"new_Title": "string",
"new_Quantity": "int",
"new_Amount": "decimal",
"new_Completed": "bool",
"new_Priority": Priority,
}
table_info = backoff(lambda: client.tables.create(table_name, columns))
print(f"[OK] Created table: {table_info.get('table_schema_name')}")
print(f" Columns created: {', '.join(table_info.get('columns_created', []))}")
# ============================================================================
# 3. CREATE OPERATIONS
# ============================================================================
print("\n" + "=" * 80)
print("3. Create Operations")
print("=" * 80)
# Single create
log_call(f"client.records.create('{table_name}', {{...}})")
single_record = {
"new_Title": "Complete project documentation",
"new_Quantity": 5,
"new_Amount": 1250.50,
"new_Completed": False,
"new_Priority": Priority.MEDIUM,
}
id1 = backoff(lambda: client.records.create(table_name, single_record))
print(f"[OK] Created single record: {id1}")
# Multiple create
log_call(f"client.records.create('{table_name}', [{{...}}, {{...}}, {{...}}])")
multiple_records = [
{
"new_Title": "Review code changes",
"new_Quantity": 10,
"new_Amount": 500.00,
"new_Completed": True,
"new_Priority": Priority.HIGH,
},
{
"new_Title": "Update test cases",
"new_Quantity": 8,
"new_Amount": 750.25,
"new_Completed": False,
"new_Priority": Priority.LOW,
},
{
"new_Title": "Deploy to staging",
"new_Quantity": 3,
"new_Amount": 2000.00,
"new_Completed": False,
"new_Priority": Priority.HIGH,
},
]
ids = backoff(lambda: client.records.create(table_name, multiple_records))
print(f"[OK] Created {len(ids)} records: {ids}")
# ============================================================================
# 4. READ OPERATIONS
# ============================================================================
print("\n" + "=" * 80)
print("4. Read Operations")
print("=" * 80)
# Single read by ID
log_call(f"client.records.get('{table_name}', '{id1}')")
record = backoff(lambda: client.records.get(table_name, id1))
print("[OK] Retrieved single record:")
print(
json.dumps(
{
"new_walkthroughdemoid": record.get("new_walkthroughdemoid"),
"new_title": record.get("new_title"),
"new_quantity": record.get("new_quantity"),
"new_amount": record.get("new_amount"),
"new_completed": record.get("new_completed"),
"new_priority": record.get("new_priority"),
"new_priority@FormattedValue": record.get("new_priority@OData.Community.Display.V1.FormattedValue"),
},
indent=2,
)
)
# Multiple read with filter
log_call(f"client.records.get('{table_name}', filter='new_quantity gt 5')")
all_records = []
records_iterator = backoff(lambda: client.records.get(table_name, filter="new_quantity gt 5"))
for page in records_iterator:
all_records.extend(page)
print(f"[OK] Found {len(all_records)} records with new_quantity > 5")
for rec in all_records:
print(f" - new_Title='{rec.get('new_title')}', new_Quantity={rec.get('new_quantity')}")
# ============================================================================
# 5. UPDATE OPERATIONS
# ============================================================================
print("\n" + "=" * 80)
print("5. Update Operations")
print("=" * 80)
# Single update
log_call(f"client.records.update('{table_name}', '{id1}', {{...}})")
backoff(lambda: client.records.update(table_name, id1, {"new_Quantity": 100}))
updated = backoff(lambda: client.records.get(table_name, id1))
print(f"[OK] Updated single record new_Quantity: {updated.get('new_quantity')}")
# Multiple update (broadcast same change)
log_call(f"client.records.update('{table_name}', [{len(ids)} IDs], {{...}})")
backoff(lambda: client.records.update(table_name, ids, {"new_Completed": True}))
print(f"[OK] Updated {len(ids)} records to new_Completed=True")
# ============================================================================
# 6. PAGING DEMO
# ============================================================================
print("\n" + "=" * 80)
print("6. Paging Demo")
print("=" * 80)
# Create 20 records for paging
log_call(f"client.records.create('{table_name}', [20 records])")
paging_records = [
{
"new_Title": f"Paging test item {i}",
"new_Quantity": i,
"new_Amount": i * 10.0,
"new_Completed": False,
"new_Priority": Priority.LOW,
}
for i in range(1, 21)
]
paging_ids = backoff(lambda: client.records.create(table_name, paging_records))
print(f"[OK] Created {len(paging_ids)} records for paging demo")
# Query with paging
log_call(f"client.records.get('{table_name}', page_size=5)")
print("Fetching records with page_size=5...")
paging_iterator = backoff(lambda: client.records.get(table_name, orderby=["new_Quantity"], page_size=5))
for page_num, page in enumerate(paging_iterator, start=1):
record_ids = [r.get("new_walkthroughdemoid")[:8] + "..." for r in page]
print(f" Page {page_num}: {len(page)} records - IDs: {record_ids}")
# ============================================================================
# 7. SQL QUERY
# ============================================================================
print("\n" + "=" * 80)
print("7. SQL Query")
print("=" * 80)
log_call(f"client.query.sql('SELECT new_title, new_quantity FROM {table_name} WHERE new_completed = 1')")
sql = f"SELECT new_title, new_quantity FROM new_walkthroughdemo WHERE new_completed = 1"
try:
results = backoff(lambda: client.query.sql(sql))
print(f"[OK] SQL query returned {len(results)} completed records:")
for result in results[:5]: # Show first 5
print(f" - new_Title='{result.get('new_title')}', new_Quantity={result.get('new_quantity')}")
except Exception as e:
print(f"[WARN] SQL query failed (known server-side bug): {str(e)}")
# ============================================================================
# 8. PICKLIST LABEL CONVERSION
# ============================================================================
print("\n" + "=" * 80)
print("8. Picklist Label Conversion")
print("=" * 80)
log_call(f"client.records.create('{table_name}', {{'new_Priority': 'High'}})")
label_record = {
"new_Title": "Test label conversion",
"new_Quantity": 1,
"new_Amount": 99.99,
"new_Completed": False,
"new_Priority": "High", # String label instead of int
}
label_id = backoff(lambda: client.records.create(table_name, label_record))
retrieved = backoff(lambda: client.records.get(table_name, label_id))
print(f"[OK] Created record with string label 'High' for new_Priority")
print(f" new_Priority stored as integer: {retrieved.get('new_priority')}")
print(f" new_Priority@FormattedValue: {retrieved.get('new_priority@OData.Community.Display.V1.FormattedValue')}")
# ============================================================================
# 9. COLUMN MANAGEMENT
# ============================================================================
print("\n" + "=" * 80)
print("9. Column Management")
print("=" * 80)
log_call(f"client.tables.add_columns('{table_name}', {{'new_Notes': 'string'}})")
created_cols = backoff(lambda: client.tables.add_columns(table_name, {"new_Notes": "string"}))
print(f"[OK] Added column: {created_cols[0]}")
# Delete the column we just added
log_call(f"client.tables.remove_columns('{table_name}', ['new_Notes'])")
backoff(lambda: client.tables.remove_columns(table_name, ["new_Notes"]))
print(f"[OK] Deleted column: new_Notes")
# ============================================================================
# 10. DELETE OPERATIONS
# ============================================================================
print("\n" + "=" * 80)
print("10. Delete Operations")
print("=" * 80)
# Single delete
log_call(f"client.records.delete('{table_name}', '{id1}')")
backoff(lambda: client.records.delete(table_name, id1))
print(f"[OK] Deleted single record: {id1}")
# Multiple delete (delete the paging demo records)
log_call(f"client.records.delete('{table_name}', [{len(paging_ids)} IDs])")
job_id = backoff(lambda: client.records.delete(table_name, paging_ids))
print(f"[OK] Bulk delete job started: {job_id}")
print(f" (Deleting {len(paging_ids)} paging demo records)")
# ============================================================================
# 11. CLEANUP
# ============================================================================
print("\n" + "=" * 80)
print("11. Cleanup")
print("=" * 80)
log_call(f"client.tables.delete('{table_name}')")
try:
backoff(lambda: client.tables.delete(table_name))
print(f"[OK] Deleted table: {table_name}")
except Exception as ex: # noqa: BLE001
code = getattr(getattr(ex, "response", None), "status_code", None)
if isinstance(ex, (requests.exceptions.HTTPError, MetadataError)) and code == 404:
print(f"[OK] Table removed: {table_name}")
else:
raise
# ============================================================================
# SUMMARY
# ============================================================================
print("\n" + "=" * 80)
print("Walkthrough Complete!")
print("=" * 80)
print("\nDemonstrated operations:")
print(" [OK] Table creation with multiple column types")
print(" [OK] Single and multiple record creation")
print(" [OK] Reading records by ID and with filters")
print(" [OK] Single and multiple record updates")
print(" [OK] Paging through large result sets")
print(" [OK] SQL queries")
print(" [OK] Picklist label-to-value conversion")
print(" [OK] Column management")
print(" [OK] Single and bulk delete operations")
print(" [OK] Table cleanup")
print("=" * 80)
if __name__ == "__main__":
main()