Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Custom API for Item details with Pictures

Posted on by 97
Hi All,

We are using On-Prem Business central,

i want to create a custom API which will send item detail , sales price detail and item picture with picture URL or binary string. i tried but in the picture content we are receiving only the media link URL and i don't know i to use this media link to show data in my web page, actually another application consuming this api.

below is my code.
 
query 50112 GetCustomerWithOrder
{
    QueryType = API;
    APIPublisher = 'QueryV1';
    APIGroup = 'QueryGroup';
    EntityName = 'Item';
    EntitySetName = 'Items';
 
    elements
    {
        dataitem(ItemDetails; Item)
        {
            column(ItemNo; "No.")
            {
 
            }
            column(Name; Description)
            {
 
            }
            column(Inventory; Inventory)
            {
 
            }
            column(Picture; Picture)
            {
 
            }
            column(Base_Unit_Price; "Unit Price")
            {
 
            }
            dataitem(Pricelist; "Sales Price")
            {
                DataItemLink = "Item No." = ItemDetails."No.";
                SqlJoinType = LeftOuterJoin;
                column(Unit_Price; "Unit Price")
                {
 
                }
                dataitem(pictures; "Picture Entity")
                {
                    DataItemLink = Id = ItemDetails.Picture;
                    SqlJoinType = LeftOuterJoin;
                    column(PictureContent; Content)
                    {
 
                    }
                }
            }
 
        }
    }
 
    var
        myInt: Integer;
 
    trigger OnBeforeOpen()
    begin
 
    end;
}
 
if anyone have any idea please let me know or you have any code snip you can share with me.

Thanks for your help.
 
  • Suggested answer
    Umut Dogan Koc Profile Picture
    Umut Dogan Koc 87 on at
    Custom API for Item details with Pictures
    Hello,
     
    You need to convert pictures into base64 format in API and consuming application should convert it back from base64 to picture.
    I would suggest you to use page API instead of query API. 
    Pictures are stored in Tenant Media record, so you need to get Tenant Media record for the picture. And content should be converted to Base64.
     local procedure SetValuesFromMediaID(MediaID: Guid)
        var
            TenantMedia: Record "Tenant Media";
        begin
            // TODO: This code should be replaced once we get a proper platform support
            // We should not build dependencies to TenantMedia table
            if IsNullGuid(MediaID) then
                exit;
     
            TenantMedia.SetAutoCalcFields(Content);
            if not TenantMedia.Get(MediaID) then
                exit;
     
            "Mime Type" := TenantMedia."Mime Type";
            Width := TenantMedia.Width;
            Height := TenantMedia.Height;
     
            Content := TenantMedia.Content;
        end;
     
    I'm putting a sample code works for company information picture, you need to fit this to tenant media:
     procedure GetCompanyLogoBase64(): Text
        var
            Base64: Codeunit "Base64 Convert";
            Tempblob: Codeunit "Temp Blob";
            OutS: OutStream;
            InS: InStream;
            Base64Txt: Text;
        begin
            rec.CalcFields(Picture);
            if Picture.HasValue then begin
                Tempblob.FromRecord(Rec, Rec.FieldNo(Picture));
                Tempblob.CreateInStream(InS);
                Base64Txt := Base64.ToBase64(InS);
                exit(Base64Txt);
            end;
        end;
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 71,656 Super User 2024 Season 2 on at
    Custom API for Item details with Pictures
    Hi, When you open this table directly, is there any data?
    The normal practice is Picture (Item table) ->  Content (table 2000000184 "Tenant Media")
     
    Hope this helps.
    Thanks.
    ZHU

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,900 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,297 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans