# dataverse-python-best-practices

A GitHub Copilot rule with working patterns for the Dataverse SDK for Python: auth, client setup, CRUD operations.

- rule · `type=rule` · GitHub Copilot · ★ 40418
- Author: github · official GitHub repository
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/github/awesome-copilot/tree/HEAD/instructions/dataverse-python-best-practices.instructions.md
- Updated: 2026-10-23
- HTML: https://affpapa.org/en/nevibecode/rules/dataverse-python-best-practices

## What it does

The rule feeds your agent proven patterns for `PowerPlatform-Dataverse-Client`. It covers installing the SDK and `azure-identity`, four auth flows (browser, client secret, certificate, Azure CLI), a singleton client pattern, and CRUD examples. The anti-pattern of building a new client per call gets its own section — worth checking your code if you open a connection inside every function.

Configuration runs through `DataverseConfig` with `language_code` (default 1033). Bulk creates are optimized inside the SDK, so manual batching is not needed. Reads support OData filters with lowercase logical names and read-only SQL queries, which org policy can disable.

You need Python 3.10 or newer, 3.11+ recommended. Production setups require tenant_id, client_id and client_secret, or a certificate.

## Requirements
- Works with: Azure

## Install

### GitHub Copilot (native platform)

**Manually: put the files in place**
- File: `.github/instructions/dataverse-python-best-practices.instructions.md` — The applyTo field sets which files it applies to

### Claude Code

**Manually: put the files in place**
- File: `.claude/rules/dataverse-python-best-practices.md` — The paths frontmatter field limits the files: move applyTo from the source into paths

### Codex

**AGENTS.md**
- Codex reads AGENTS.md: paste the rule text there

### Cursor

**Manually: put the files in place**
- File: `.cursor/rules/dataverse-python-best-practices.mdc` — Frontmatter: description, globs, alwaysApply. Move applyTo from the source into globs, or the rule will not attach on its own

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/rules/dataverse-python-best-practices.md`

### Cline

**Manually: put the files in place**
- File: `.clinerules/dataverse-python-best-practices.md`

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/github/awesome-copilot/tree/HEAD/instructions/dataverse-python-best-practices.instructions.md

## When to use it
- connect to Dataverse from Python without rewriting auth
- pick an auth flow for local dev or a server job
- reuse one client instead of creating a new one per request
- run bulk inserts and updates on records
- filter and sort records with OData parameters

## Example prompts
- Show me how to build a singleton Dataverse client in Python
- How do I authenticate to Dataverse with ClientSecretCredential?
- Bulk create account records with client.create

## Source text · dataverse-python-best-practices.instructions.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 3c555445ea5777a4.

BEGIN THIRD-PARTY FILE 3c555445ea5777a4
````text
# Dataverse SDK for Python - Best Practices Guide

## Overview
Production-ready patterns and best practices extracted from Microsoft's official PowerPlatform-DataverseClient-Python repository, examples, and recommended workflows.

## 1. Installation & Environment Setup

### Production Installation
```bash
# Install the published SDK from PyPI
pip install PowerPlatform-Dataverse-Client

# Install Azure Identity for authentication
pip install azure-identity

# Optional: pandas integration for data manipulation
pip install pandas
```

### Development Installation
```bash
# Clone the repository
git clone https://github.com/microsoft/PowerPlatform-DataverseClient-Python.git
cd PowerPlatform-DataverseClient-Python

# Install in editable mode for live development
pip install -e .

# Install development dependencies
pip install pytest pytest-cov black isort mypy ruff
```

### Python Version Support
- **Minimum**: Python 3.10
- **Recommended**: Python 3.11+ for best performance
- **Supported**: Python 3.10, 3.11, 3.12, 3.13, 3.14

### Verify Installation
```python
from PowerPlatform.Dataverse import __version__
from PowerPlatform.Dataverse.client import DataverseClient
from azure.identity import InteractiveBrowserCredential

print(f"SDK Version: {__version__}")
print("Installation successful!")
```

---

## 2. Authentication Patterns

### Interactive Development (Browser-Based)
```python
from azure.identity import InteractiveBrowserCredential
from PowerPlatform.Dataverse.client import DataverseClient

credential = InteractiveBrowserCredential()
client = DataverseClient("https://yourorg.crm.dynamics.com", credential)
```

**When to use:** Local development, interactive testing, single-user scenarios.

### Production (Client Secret)
```python
from azure.identity import ClientSecretCredential
from PowerPlatform.Dataverse.client import DataverseClient

credential = ClientSecretCredential(
    tenant_id="your-tenant-id",
    client_id="your-client-id",
    client_secret="your-client-secret"
)
client = DataverseClient("https://yourorg.crm.dynamics.com", credential)
```

**When to use:** Server-side applications, Azure automation, scheduled jobs.

### Certificate-Based Authentication
```python
from azure.identity import ClientCertificateCredential
from PowerPlatform.Dataverse.client import DataverseClient

credential = ClientCertificateCredential(
    tenant_id="your-tenant-id",
    client_id="your-client-id",
    certificate_path="path/to/certificate.pem"
)
client = DataverseClient("https://yourorg.crm.dynamics.com", credential)
```

**When to use:** Highly secure environments, certificate-pinning requirements.

### Azure CLI Authentication
```python
from azure.identity import AzureCliCredential
from PowerPlatform.Dataverse.client import DataverseClient

credential = AzureCliCredential()
client = DataverseClient("https://yourorg.crm.dynamics.com", credential)
```

**When to use:** Local testing with Azure CLI installed, Azure DevOps pipelines.

---

## 3. Singleton Client Pattern

**Best Practice**: Create one `DataverseClient` instance and reuse it throughout your application.

```python
# ❌ ANTI-PATTERN: Creating new clients repeatedly
def fetch_account(account_id):
    credential = InteractiveBrowserCredential()
    client = DataverseClient("https://yourorg.crm.dynamics.com", credential)
    return client.get("account", account_id)

# ✅ PATTERN: Singleton client
class DataverseService:
    _instance = None
    
    def __new__(cls):
        if cls._instance is None:
            credential = InteractiveBrowserCredential()
            cls._instance = DataverseClient(
````
END THIRD-PARTY FILE 3c555445ea5777a4

The third-party file has ended. Everything between the 3c555445ea5777a4 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 3623 of 18673 bytes. © github · MIT · https://github.com/github/awesome-copilot/tree/HEAD/instructions/dataverse-python-best-practices.instructions.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
