from enum import IntEnum
from azure.identity import InteractiveBrowserCredential
from PowerPlatform.Dataverse.client import DataverseClient
from PowerPlatform.Dataverse.core.errors import DataverseError, MetadataError
class TaskStatus(IntEnum):
NEW = 1
IN_PROGRESS = 2
COMPLETED = 3
class TaskPriority(IntEnum):
LOW = 1
MEDIUM = 2
HIGH = 3
# Setup
credential = InteractiveBrowserCredential()
client = DataverseClient("https://yourorg.crm.dynamics.com", credential)
try:
# 1. Create table
print("Creating table...")
table_info = client.create_table(
"new_ProjectTask",
primary_column_schema_name="new_Title",
columns={
"new_Description": "string",
"new_Status": TaskStatus,
"new_Priority": TaskPriority,
"new_DueDate": "datetime",
"new_EstimatedHours": "decimal"
}
)
print(f"✓ Created table: {table_info['table_schema_name']}")
# 2. Create records
print("\nCreating tasks...")
tasks = [
{
"new_Title": "Design system",
"new_Description": "Create design system architecture",
"new_Status": TaskStatus.NEW,
"new_Priority": TaskPriority.HIGH,
"new_EstimatedHours": 40.0
},
{
"new_Title": "Implement UI",
"new_Description": "Build React components",
"new_Status": TaskStatus.IN_PROGRESS,
"new_Priority": TaskPriority.HIGH,
"new_EstimatedHours": 80.0
},
{
"new_Title": "Write tests",
"new_Description": "Unit and integration tests",
"new_Status": TaskStatus.NEW,
"new_Priority": TaskPriority.MEDIUM,
"new_EstimatedHours": 30.0
}
]
task_ids = client.create("new_ProjectTask", tasks)
print(f"✓ Created {len(task_ids)} tasks")
# 3. Query and filter
print("\nQuerying high-priority tasks...")
high_priority = client.get(
"new_ProjectTask",
filter="new_priority eq 3",
select=["new_Title", "new_Priority", "new_EstimatedHours"]
)
for page in high_priority:
for task in page:
print(f" - {task['new_title']}: {task['new_estimatedhours']} hours")
# 4. Update records
print("\nUpdating task status...")
client.update("new_ProjectTask", task_ids[1], {
"new_Status": TaskStatus.COMPLETED,
"new_EstimatedHours": 85.5
})
print("✓ Updated task status")
# 5. Cleanup
print("\nCleaning up...")
client.delete_table("new_ProjectTask")
print("✓ Deleted table")
# Clear cache
client.flush_cache()
except (MetadataError, DataverseError) as e:
print(f"❌ Error: {e}")