Hi Folks,
This is in continuation in the Python + Dataverse series, it is worth checking out from the start of this series here.
At times, there will be a need to remove PII(Personally Identifiable Information) present in the Dataverse Environments, for this one time task, you can easily run Python script below, let’s take example of removing PII from Contact fields in the below example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from azure.identity import InteractiveBrowserCredential | |
| from PowerPlatform.Dataverse.client import DataverseClient | |
| # Connect to Dataverse | |
| credential = InteractiveBrowserCredential() | |
| client = DataverseClient("https://ecellorsdev.crm8.dynamics.com", credential) | |
| #use AI to remove PII data from the dataverse records, let's say contact records | |
| def remove_pii_from_contact(contact): | |
| pii_fields = ['emailaddress1', 'telephone1', 'mobilephone', 'address1_line1', 'address1_city', 'address1_postalcode'] | |
| for field in pii_fields: | |
| if field in contact: | |
| contact[field] = '[REDACTED]' | |
| return contact | |
| # Fetch contacts with PII (Dataverse client returns paged batches) | |
| contact_batches = client.get( | |
| "contact", | |
| select=[ | |
| "contactid", | |
| "fullname", | |
| "emailaddress1", | |
| "telephone1", | |
| "mobilephone", | |
| "address1_line1", | |
| "address1_city", | |
| "address1_postalcode", | |
| ], | |
| top=10, | |
| ) | |
| # Remove PII and update contacts | |
| for batch in contact_batches: | |
| for contact in batch: | |
| contact_id = contact.get("contactid") | |
| sanitized_contact = remove_pii_from_contact(contact) | |
| # Prepare update data (exclude contactid) | |
| update_data = {key: value for key, value in sanitized_contact.items() if key != "contactid"} | |
| # Update the contact in Dataverse | |
| client.update("contact", contact_id, update_data) | |
| print(f"Contact {contact_id} updated with sanitized data: {sanitized_contact}") |
If you want to work on this, download the Python Notebook to use in VS Code…
Cheers,
PMDY
