dynamics crm 365 plugin apply QueryExpression to custom list of entities - dynamics-crm

In my plugin, I have a list of entities. I would like to apply the query expression that is passed in the input parameters
QueryExpression query = PluginExecutionContext.InputParameters["Query"];
to a custom list of type entity.
List<Entity> myList;
The entities in the list match the same attributes as the entities used in the plugin context. Is there a way to apply a QueryExpression to a list of entities, or convert a QueryExpression to linq?

QueryExpression is really just a wrapper around FetchXML, which is just an XML schema for queries in Dynamics CRM. If you want to pass in a query as a parameter to a plugin, you could set up an custom entity call "query" or something to that effect and add a field of type textarea to that custom entity called "fetchxml". Then set up the input parameter of your plugin to accept a record of that custom entity instead of a text parameter. this has the added benefit of allowing you to more easily edit the input parameters of the plugin.
Of course, you could always just put the raw fetchXML into the parameter as text, but I can tell you from experience that this will come back to bite you as it is extremely hard to maintain because any changes elsewhere in the system could completely trash your plugin.
If you want to know more about how to get the fetchXML for a certain query or have any other questions, just shoot me a comment.

Is there a way to apply a QueryExpression to a list of entities
Answer is No.
QueryExpression & FetchXML is native of Dynamics CRM, it can be used against CRM service (Database) only. I assume this plugin is on Retrieve message and you are trying to use the system's query expression from that Retrieve service call against your own dataset of entity (List<Entity>). But why?
, or convert a QueryExpression to linq?
No. I know the reverse is possible.

Related

Filtering across multiple Dynamics entities in Common Data Service

I'm trying to query our Dynamics 365 CRM through the Common Data Service / Microsoft Dataverse. I can manage this when I need to just filter on fields that exist within the entity type I'm searching. But I also need to filter on a different linked entity.
So far I can query on the contact entity just fine:
var contacts = svc.Get("contacts?" +
"$select=firstname,lastname,address1_name,address1_line1," +
"address1_postalcode,emailaddress1,statecode,_parentcustomerid_value&" +
$"$filter=contains(firstname,'{firstName}') and " +
$"contains(lastname, '{lastName}')"
, formattedValueHeaders);
And this returns a list of contacts which match the filter.
However, I also need to filter on the Account Name that the contact is linked to. If I had the AccountId value then I could do this by adding $"contains(_parentcustomerid_value, '{accountId}')" to the filter. But I can't use this as the input data I'm receiving to query on is the name of the account and not an Id value.
Any ideas on how to do this?
Currently, the only way I can see is to use my existing query, then to loop through each contact and check the returned account name in the response, and if this matches then in effect I've filtered it. But I would prefer to be able to have the whole filter all in a single query.
You can use this to filter based on lookup field name value.
https://devorg.crm.dynamics.com/api/data/v9.1/contacts?$filter=parentcustomerid_account/name eq 'test'

How to distinguish entities in Dynamics CRM using the web API?

I am using the request to GET "/api/data/v9.0/EntityDefinitions" to list all the entities that are present in Dynamics and get basic information regarding them (EntityMetadata EntityType).
I know that some of the entities are standard, custom, non-createable, non-updateable, etc.
To check if an entity is a custom I can use a field named IsCustom from EntityDefinition and if it is true then the entity is custom. But for non-createable entities and others (I don't know how to call them properly) this approach is not applicable, because there is no such field.
For example, 'activitypointer' supports only GET operation (activitypointer EntityType). Records of this type cannot be created. But from the request above, I cannot get the correct information about that.
Can entities be distinguished?
P.S.: maybe I did something wrong and need to look elsewhere?

How to retrieve field values and its translations from Microsoft dynamics CRM using REST API

I need to retrieve the entities from my CRM site and all the fields associated with that entity. Need to get the translated values as well.
Please provide some queries that will be helpful in this case
I tried with below queries, but this could not fetch all the values.
..../api/data/v9.1/EntityDefinitions(LogicalName='account')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet,GlobalOptionSet
..../api/data/v9.1/EntityDefinitions(LogicalName='account')/Attributes
..../api/data/v9.1/GlobalOptionSetDefinitions
You can retrieve CRM entities by calling:
[Organization URI]/api/data/v9.1/
In order to get an entity's attributes you can call:
[Organization URI]/api/data/v9.1/EntityDefinitions(LogicalName='[Entity name (ex: account)]')/Attributes
For attribute translation, navigate to 'DisplayName' you will find 'LocalizedLabels'.
For more information you can check this url:
https://learn.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/query-metadata-web-api

Can I use FetchXML to retrieve a list of Entities and a list of Attributes?

I want a fetchxml that will return a list of Entities, hopefully with Display name and schema name.
Similarly I would like a fetchxml that will return the attributes for an Entity. Also with Display name and Schema name.
I assume there is a way because tools in the XRMToolBox load dropdowns with those value.
Fetchxml is DML query to retrieve data from backend & cannot be used for metadata retrieval. We have to use Organization service or web api to pull Metadata like Entity & attribute definitions.
SO thread 1 & thread 2 will help you to achieve it.
Other useful links:
Query metadata using the Web API
Retrieve data with queries using SDK assemblies
Use the Organization Service to read and write data or metadata

web api odata service - return complex type

i want to create web api odata service that return reault type which consist of collection data member and additional members like this service return:
http://services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq 'Redmond'
as you can see the result type is consist of collection data member and additional members
can anyone send me a sample how to do it?
i can't succeed to create this kind of complex type and to be able filter the collection items by there values
as yuo can see in this query it return all the result without filter the items
services.odata.org/OData/OData.svc/Suppliers
i want to be able filter this type like this:
services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq 'Redmond'
in this query i managed to filter the collection member items and still returning the other data members.
If you just want to implement filters like: services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq 'Redmond'
Please check the sample at http://aspnet.codeplex.com/SourceControl/changeset/view/903afc4e11df#Samples%2fNet4%2fCS%2fWebApi%2fODataServiceSample%2fReadMe.txt
It has a supplier and address model with queryable attribute. It should work with the same $filter query.
Odata support was implicit in asp.net webapi RC.
You just had to return a IQueryable from you Actions and mark it with [QueryableAttribute].
Only this much supported querystring based data filtering.
I was a bit disappointed when I saw the [QueryableAttribute] doesnt build in RTM.
In RTM it’s available as a separate package, Microsoft.AspNet.WebApi.OData on Nuget in a preview/alpha form. Full release is coming later this fall. You can grab it from here(http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData). There is a nice overview post available (http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx)

Resources