Skip to main content

Notifications

Announcements

No record found.

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

Custom API for Item details with Pictures

(1) ShareShare
ReportReport
Posted on by 144
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 74,148 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,431 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,503 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans