Retrieve all personal views containing a specific field

In some cases, you want to delete a custom field in a highly used entity. You can easily identify where it appears in system views, forms, etc. But since it’s, many users probably have personal views that include it. So you have to alert those users to remove it from their views, so they don’t  get an error message later on after you delete the field. 

If a field is deleted and used on a personal view, the following error is displayed on the view: 0x80041103 Query Builder Error

I suggest using a console application with the Impersonation principal to know exactly the personal views that use a given field. Indeed, all personal views are stored at the userQuery entity.

Doc: “Impersonation is used to execute business logic (code) on behalf of another Dynamics 365 for Customer Engagement user to provide a desired feature or service using the appropriate role and object-based security of that impersonated user.”

In the code below, I display all Contact’s personal views using the custom field “mel_contactnumber” and their owner.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.ServiceModel.Description;
namespace MelTools
class getAllUsersPersonalViewsContainingField
static void Main(string[] args)
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "xxx";
clientCredentials.UserName.Password = "xxx";
OrganizationServiceProxy organizationService = new OrganizationServiceProxy(new Uri(""), null, clientCredentials, null);
// Instantiate QueryExpression QEsystemuser
var QEsystemuser = new QueryExpression("systemuser");
// Add all columns to QEsystemuser.ColumnSet
QEsystemuser.ColumnSet.AllColumns = true;
EntityCollection users = organizationService.RetrieveMultiple(QEsystemuser);
foreach (var user in users.Entities)
organizationService.CallerId = user.Id;
// Define Condition Values
var QEuserquery_fetchxml_entity = "%<entity name=\"contact\">%";
var QEuserquery_fetchxml_field= "%<attribute name=\"mel_contactnumber\"/>%";
// Instantiate QueryExpression QEuserquery
var QEuserquery = new QueryExpression("userquery");
// Add all columns to QEuserquery.ColumnSet
QEuserquery.ColumnSet.AllColumns = true;
// Define filter QEuserquery.Criteria
QEuserquery.Criteria.AddCondition("fetchxml", ConditionOperator.Like, QEuserquery_fetchxml_entity);
QEuserquery.Criteria.AddCondition("fetchxml", ConditionOperator.Like, QEuserquery_fetchxml_field);
EntityCollection personalViews = organizationService.RetrieveMultiple(QEuserquery);
if (personalViews.Entities.Count > 0)
foreach (var personalView in personalViews.Entities)
Console.WriteLine("view name: " + personalView.GetAttributeValue<string>("name") + "| owner: " + personalView.GetAttributeValue<EntityReference>("ownerid").Name);
catch (Exception e) { }
Console.WriteLine("click to exit");


One thought on “Retrieve all personal views containing a specific field

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s