Saturday, 5 August 2017

CRUD with Odata and Data entities in Dynamics 365 for operations - READ Operations

Before starting anything, I assume that you are already aware of Data Entity concepts in D365O.If not, please go through the link below
Data Entites

Also, If you want to see what is Odata and what it does in D365O, click on below link
Odata in D365

By this time, you are already aware that Data Entities follows Odata protocols and present you the data in Json format.
So, how actually following up of Odata helps us?
It actually has lot of positiveness other than just representation of data. As per the Odata protocol definition, it supports Create, Read, Update and Delete(CRUD)-style operations and so it can be used superbly for integration with external systems.

Of course, before proceeding ahead i must tell you that, to access anything outside of D365O you need access token(Bearer token) which I have already explained on my previous post
Get Token to expose data outside of D365 for Operations

Now, the question is how does CRUD actually works for D365O data entities. I will be explaining each operation one by one.

I have created a data entity named as "BeaconEntity" for this purpose, but you can follow same process for any of the available public entity also.
I will be using POSTMAN tool to try out all these operations. you can opt to use Fiddler(as it is used by MS). You can also use other tools as well as your coding skills in other languages to test out all these things.
Just for testing of READ Operations, you can use the 2nd tab of browser where  you are already logged in to D365O and if its session is not yet expired.

Before proceeding ahead, you might consider getting a fullmetadata.
https://********devaos.cloudax.dynamics.com/data/Beaconservice?$format=application/json;odata.metadata=full


{
    "@odata.context": "https://************devaos.cloudax.dynamics.com/data/$metadata#Beaconservice",
    "value": [
        {
            "@odata.type": "#Microsoft.Dynamics.DataEntities.BeaconService",
            "@odata.id": "https://******devaos.cloudax.dynamics.com/data/Beaconservice(dataAreaId='usrt',BeaconNum='123456789')",
            "@odata.etag": "W/\"JzE0NTk5MjkxNjcsNTYzNzE0NDU3Nic=\"",
            "@odata.editLink": "Beaconservice(dataAreaId='usrt',BeaconNum='123456789')",
            "Description": "beacon 1",
            "dataAreaId": "usrt",
            "BeaconNum": "123456789",
            "EffectiveDate@odata.type": "#DateTimeOffset",
            "EffectiveDate": "2017-01-08T12:00:00Z",
            "BeaconStatus@odata.type": "#Microsoft.Dynamics.DataEntities.BeaconStatus",
            "BeaconStatus": "Active",
            "Brand": "Brand1",
            "StartTime": 0,
            "Category": "Category1",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "Offer1"
        },
        {
            "@odata.type": "#Microsoft.Dynamics.DataEntities.BeaconService",
            "@odata.id": "https://***********devaos.cloudax.dynamics.com/data/Beaconservice(dataAreaId='usrt',BeaconNum='234567891')",
            "@odata.etag": "W/\"Jzg0ODAyMzczOCw1NjM3MTQ0NTc3Jw==\"",
            "@odata.editLink": "Beaconservice(dataAreaId='usrt',BeaconNum='234567891')",
            "Description": "Beacon 2",
            "dataAreaId": "usrt",
            "BeaconNum": "234567891",
            "EffectiveDate@odata.type": "#DateTimeOffset",
            "EffectiveDate": "2017-01-08T12:00:00Z",
            "BeaconStatus@odata.type": "#Microsoft.Dynamics.DataEntities.BeaconStatus",
            "BeaconStatus": "InActive",
            "Brand": "Brand2",
            "StartTime": 0,
            "Category": "Category2",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "offer2"
        },
        {
            "@odata.type": "#Microsoft.Dynamics.DataEntities.BeaconService",
            "@odata.id": "https://*********devaos.cloudax.dynamics.com/data/Beaconservice(dataAreaId='usrt',BeaconNum='345678912')",
            "@odata.etag": "W/\"JzEsNTYzNzE0NDU3OCc=\"",
            "@odata.editLink": "Beaconservice(dataAreaId='usrt',BeaconNum='345678912')",
            "Description": "Beacon 3",
            "dataAreaId": "usrt",
            "BeaconNum": "345678912",
            "EffectiveDate@odata.type": "#DateTimeOffset",
            "EffectiveDate": "2017-05-08T12:00:00Z",
            "BeaconStatus@odata.type": "#Microsoft.Dynamics.DataEntities.BeaconStatus",
            "BeaconStatus": "Active",
            "Brand": "Brand3",
            "StartTime": 0,
            "Category": "Category3",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "Offer3"
        }
    ]
}

READ(R):-
oh, there should be CREATE operation 1st, did i forgot the sequence, no i didn't. As READ is the most basic operation without doing much and so i have mentioned it 1st.
To read the data, you just need 3 things
1. Make the entity public
2. Get access(bearer) token.
3. Have a name of Entity with you.

open Postman, paste your hostURI succeeded by /data/<entityName>. 
Please note that for all Read operations, method type should always be GET.
Pass the key as "Authorization" and paste its value as <bearer token> you already have.


Then click on Send button to get the available data from D365O based on the user rights and default company(for me its USRT) assigned to him.
https://*********devaos.cloudax.dynamics.com/data/Beaconservice
I have 3 records in table and so the output looks like below:

{
    "@odata.context": "https://*********devaos.cloudax.dynamics.com/data/$metadata#Beaconservice",
    "value": [
        {
            "@odata.etag": "W/\"JzE0NTk5MjkxNjcsNTYzNzE0NDU3Nic=\"",
            "Description": "beacon 1",
            "dataAreaId": "usrt",
            "BeaconNum": "123456789",
            "EffectiveDate": "2017-01-08T12:00:00Z",
            "BeaconStatus": "Active",
            "Brand": "Brand1",
            "StartTime": 0,
            "Category": "Category1",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "Offer1"
        },
        {
            "@odata.etag": "W/\"Jzg0ODAyMzczOCw1NjM3MTQ0NTc3Jw==\"",
            "Description": "Beacon 2",
            "dataAreaId": "usrt",
            "BeaconNum": "234567891",
            "EffectiveDate": "2017-01-08T12:00:00Z",
            "BeaconStatus": "InActive",
            "Brand": "Brand2",
            "StartTime": 0,
            "Category": "Category2",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "offer2"
        },
        {
            "@odata.etag": "W/\"JzEsNTYzNzE0NDU3OCc=\"",
            "Description": "Beacon 3",
            "dataAreaId": "usrt",
            "BeaconNum": "345678912",
            "EffectiveDate": "2017-05-08T12:00:00Z",
            "BeaconStatus": "Active",
            "Brand": "Brand3",
            "StartTime": 0,
            "Category": "Category3",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "Offer3"
        }
    ]
}

CROSS-COMPANY:
This can come very handy if you have to get records from other company(other than the default mapped company with the user).
https://********devaos.cloudax.dynamics.com/data/Beaconservice?$filter=dataAreaId eq 'usmf' &cross-company=true

setting up 'cross-company' will give records from all the Legal entities irrespective of the default legal entity. Below is the result of my query.

{
  "@odata.context":"https://********devaos.cloudax.dynamics.com/data/$metadata#Beaconservice","value":[
    
  ]
}

Its blank, as i don't have any record in USMF.
you can also use only cross-company without any filter as shown below to get consolidated records from all the legal entities.
https://********devaos.cloudax.dynamics.com/data/Beaconservice?cross-company=true

I will dig more deep into READ operation to get required data and required format.
But, thats on Part 2 of this Post.

19 comments:

  1. I all the time emailed this website post page to all my contacts, for the reason that if like to read it next my friends will
    too.

    ReplyDelete
    Replies
    1. I am glad and surely motivated...will be posting more as soon as i'll get free from my work...

      Delete