How to get picture from MediaSet through standard APIs.
Hello, Team.
In community I saw a couple of times good question about how to get item picture in API response.
OK, challenge accepted.
Lets look at items API:
1. We need to make http request something like that
GET http://nav2017:7048/DynamicsNAV110/api/beta/companies(68a76662-a198-477b-8097-d9df2116514a)/items
nav2017 - my server
7048 - OData port
DynamicsNAV110 - NAV instance
68a76662-a198-477b-8097-d9df2116514a - my company ID
For certain item it'll look next way:
GET http://nav2017:7048/DynamicsNAV110/api/beta/companies(68a76662-a198-477b-8097-d9df2116514a)/items(a19819f4-6e65-42ee-bbd9-eba4cc3199fc)
a19819f4-6e65-42ee-bbd9-eba4cc3199fc - item ID (field 8000 of item table)
Looks great, isn't it ?
2. Items API is a API-type page 5740 Item Entity based on 27 table Item.
So it's logical try to add Picture field on it and get a image:
Now compile the page and make a request.
Oops, something gone wrong. Now we know that API doesn't understand MediaSet type fields.
So we can try to obtain BLOB by Mediaset ID and paste it into request. But where are BLOBs ? They stores at 2000000184 Tenant Media table:
3. Create a codeunit to get a BLOB in base64 format by MediaSet ID with next steps:
- Get Tenant Media by Item.Picture.Item(1) - first picture id . If you need them all - create JSON Array.
- Copy blob from Tenant Media to TempBlob record to convert it to base 64 (used instead of dotnet converter)
- Create JSON token with converted text
- Export JSON token as text
Codeunit example:
4. Create text variable which gets picture data from your codeunit and paste it on API page with page extension:
5. Publish your extension to database.
6. Run request again and enjoy:
7. To control - copy picture text value to any base64 - image converter:
Hope it was useful for you!
Comments
-
Great article! Quick question. What if I was looking to import in a picture to link with an existing item? Essentially would I just perform the converse of these steps and methods? If I read the POST method's JSON request body in order to pull the Base64-encoded image can I use a TenantMedia method to insert that image?
*This post is locked for comments