This method will, if your custom view has a unique name (within the CRM Org), get the View and execute its FetchXml returning an EntityCollection
. It should get you started with the basics which you can build out based on your more specific requirements.
This is starter/learning code only - not intended for deployment without error handling.
public EntityCollection RetrieveCustomView(string viewName, IOrganizationService service)
{
var query = new QueryExpression()
{
EntityName = "savedview",
ColumnSet = new ColumnSet("fetchxml"),
Criteria = new FilterExpression()
{
Conditions = { new ConditionExpression("name", ConditionOperator.Equal, viewName) }
}
};
var result = service.RetrieveMultiple(query);
if (result.Entities.Any())
{
var fetchXml = result.Entities[0].Attributes["fetchxml"].ToString();
var fetchQuery = new FetchExpression(fetchXml);
return service.RetrieveMultiple(fetchQuery);
}
return null;
}