Retrieve data from View based on View Name in plugin

We had requirement to update field on multiple records of an entity based on another entity flag. We need to keep it in mind that In future, If we change View of an entity then it will not affect our plugin logic.

Here we need to retrieve records from one of the active views of an entity instead of writing query directly on entity or relationship.

We have option to query on views using CRM base entity :

1. savedquery (https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/savedquery?view=dynamics-ce-odata-9)

2. userquery (https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/userquery?view=dynamics-ce-odata-9)

Note : System Views are stored in the SavedQuery entity, while User Views are stored in the UserQuery Entity.

We have system view so in our case we used “savedquery” entity instead of “userquery” entity.

Basic code to get the fetchxml of view are as below :

private string getFetchXml(IOrganizationService service)
{
var query = new QueryExpression
{
EntityName = "savedquery",
ColumnSet = new ColumnSet("savedqueryid", "name", "fetchxml"),
Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "name",
Operator = ConditionOperator.Equal,
Values = { "View Name" }//View Name - "Active Accounts"
},
new ConditionExpression
{
AttributeName = "returnedtypecode",
Operator = ConditionOperator.Equal,
Values = { "Entity Name" } //Account
}
}
}
};
var result = service.RetrieveMultiple(query);
var view = result.Entities.FirstOrDefault();
var fetchXml = view.GetAttributeValue("fetchxml");
return fetchXml;
}

One thought on “Retrieve data from View based on View Name in plugin

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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