How to get 'ownerid' column from CRM database of type `owner` using Web API approach? - dynamics-crm

I'm using Microsoft dynamics 365 - on premises of my client. Here, I am facing an issue in accessing one of the fields - ownerid of type owner.
I am using Web API approach to access the CRM data. When I hit the query in URL, it brings all the data except ownerid column.
It's field type is: Simple
Has anyone faced the same issue with WebAPI query approach?
URL Query:
https://baseURL/moves?$select=moveid,ownerid,movenumber&$filter=movenumber%20eq%20%27236659%27
Output:
{
"#odata.context":"https://baseURL/$metadata#moves(moveid,ownerid,movenumber)","value":[
{
"#odata.etag":"W/\"37655223\"","moveid":"4583b2fe-83d9-e611-80c7-369e029457bb","movenumber":"236659"
}
]
}

I think ownerid is a lookup, so you need to expand it first by using $expand query parameter, to be included in the response.
https://baseURL/moves?$expand=ownerid&$select=moveid,ownerid,movenumber&$filter=movenumber%20eq%20%27236659%27
Reference: https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/using-select-expand-and-value

Related

Facing error when I make a POST request to create entity in Dynamics CRM from Postman?

I am having trouble making a successful HTTPS Post Request to Dynamics 365 CRM customer service.
I am able to successfully place GET requests but facing issue while creating new account/contact/incident.
Request body:
{
"name": "MS account test",
"customerid_contact#odata.bind": "/contacts(904d1761-b522-ed11-9db2-000d3af06307)"
}
Please help!!
I get the following error: "An undeclared property 'customerid' which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values."
You are trying to bind contact Record.
For Account Entity, when you are trying to add contact as lookup, field name should be primarycontactid.
For incident entity it will be below 3 field
primarycontactid#odata // Lookup
msa_partnercontactid // Lookup
responsiblecontactid // Lookup
for Contact it will be
parent_contactid
for relationships you need to use the navigation property instead of the lookup name (they can be equal but is not always the case).
as you mentioned 3 different tables (account, contact, incident) you can create the request using my tool Dataverse REST Builder, inside the generated code you can see the name or you can export the collection to Postman to see the raw request.

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

How do I select Dynamics CRM reference attributes?

Please read the follows and tell me what I did wrong.
The request I sent:
https://gravityrush.crm5.dynamics.com/api/data/v9.1/accounts?$expand=ownerid/$ref
The response I expected:
"ownerid":{
"#odata.type":"#Microsoft.Dynamics.CRM.systemuser",
"#odata.id":"https://gravityrush.crm5.dynamics.com/api/data/v9.0/systemusers(41041b99-e1ae-46c6-a10c-95ce77fc0b18)"
}
The response I received:
"ownerid":{
"#odata.type":"#Microsoft.Dynamics.CRM.systemuser",
"#odata.id":"https://gravityrush.crm5.dynamics.com/api/data/v9.1/owners()"
}
Thank you.
I think it's possibly a limitation/bug related to this field type.
As you know, the ownerid field can point to a systemuser or team record, and if you take a look at the field definition you'll see that the relationship is actually to an entity called Owner, and that's why you're getting the owners() url:
"#odata.id":"/api/data/v9.1/owners()"
I tried to workaround this using the owninguser and owningteam fields, but it looks like there's an issue there as well because when I added the $ref I started to get nulls values for that field:
GET /api/data/v9.1/accounts?$select=accountid&$expand=owninguser($select=systemuserid)
"owninguser":{
"systemuserid":"f30a6a42-3067-4c78-b348-9cbc56531f5e",
"ownerid":"f30a6a42-3067-4c78-b348-9cbc56531f5e"
}
GET /api/data/v9.1/accounts?$select=accountid&$expand=owninguser/$ref
"owninguser":null
Just to be sure, I tried to do the same for a Customer field (kind of same principle where this field can point to a contact or account record) but I din't found this issue, you can get the reference to the Account/Contact using the following query:
GET /api/data/v9.1/incidents?$select=incidentid&$expand=customerid_account/$ref, customerid_contact/$ref
Try to send API query as below and you shall have the OwnerId
https://orgurl/api/data/v8.2/accounts()?$select=_ownerid_value
You will get OwnerId of each account Record.

GraphQL pre-approved queries

I read that Facebook's internal servers accept any queries in dev
mode, and these are cached. In production, only a pre-approved/cached
query is permitted. This was mentioned as a model which other servers
should adopt.
Does someone know what tools do they use for that? Does this process is described more detailed somewhere?
I don't know how it's down in facebook but I can explain how I did it in GraphQL Guru. As graphql is language agnostic I'll explain without being language specific.
The way persisted queries work is a client sends a query with a unique id and variables to a graphql (persisted query ready) server.
{
"id": "1234",
"varibles": {
"firtName": "John",
"lstName": "Smith"
}
}
For the id don't use a hash of the query as the this results in long id names of varying sizes, which kind of defeats the purpose.
On your server, create a file with the same name as the persisted query id, which contains the actual graphql query. Or save it in a database.
To get the graphql query you will need to intercept it via middleware. The middleware retrieves the graphql query via its id and passes the query on to the graphql endpoint. Depending on how the query was defined the middleware may need to parse it. Also, it is in the middleware where you can whitelist if the persisted query id does not exist.
Then the graphql endpoint process the query as normal.
You can see a nodejs example here https://github.com/otissv/guru-express-server/blob/master/src/routes/graphql-route.js

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