Skip to main content

Notifications

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

Get URL of Item Picture using custom API.

Posted on by 99
Hi All,

I am using On-Prem Business central.

I have created on custom API which is returning the item detail with item picture in base64 string. but it is taking long time to fetch data using API(45 second). Due to the large base64 string it is taking long time to process.

Does anyone have any idea how i can optimize it.

Or anyone have how idea how i can get exact URL of image which i can set in the image tag Source. eg. <img src="itemURL">.

Code : 
page 50119 "GetAllItemsDetail"
{
    PageType = API;
    APIVersion = 'v1.0';
    APIPublisher = 'QueryV1';
    APIGroup = 'QueryGroup';
    EntityName = 'Item';
    EntitySetName = 'Items';
    DelayedInsert = true;
    SourceTable = Item;
    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field(ItemNo; Rec."No.")
                {
                    Caption = 'Item No';
                }
                field(BrandName; Rec."Brand Name")
                {
                    Caption = 'Brand Name';
                }
                field(Base_Unit_Price; Rec."Unit Price")
                {
                    Caption = 'Base Unit Price';
                }
                // Picture content in base64
                field(PictureContent; GetItemPicturesAsJSON(Rec."No."))
                {
                    Caption = 'Picture Content';
                    ApplicationArea = All;
                }
            }
        }
    }
   // Procedure to get base64 string of image.
    procedure GetItemPicturesasJSON(ItemNo: Code[20]): Text;
    var
        Item: Record Item;
        TenantMedia: Record "Tenant Media";
        PictureText: Text;
        PictureInStream: InStream;
        JObject: JsonObject;
        JToken: JsonToken;
        TempBlob: Codeunit "Temp Blob";
        PictureOutstream: OutStream;
        Base64Convert: Codeunit "Base64 Convert";
    begin
        Item.get(ItemNo);
        if Item.Picture.Count = 0 then
            exit('');
        TenantMedia.get(Item.Picture.Item(1));
        TenantMedia.CalcFields(Content);
        if TenantMedia.Content.HasValue then begin
            Clear(PictureText);
            Clear(PictureInStream);
            TenantMedia.Content.CreateInStream(PictureInStream);
            TempBlob.CreateOutStream(PictureOutstream);
            CopyStream(PictureOutstream, PictureInStream);
            TempBlob.CreateInStream(PictureInStream);
            PictureText := Base64Convert.ToBase64(PictureInStream, false);
            JObject.Add('picture', PictureText);
            JObject.SelectToken('picture', JToken);
        end;
        exit(JToken.AsValue.AsText);
    end;
}


Thanks for help.
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 71,953 Super User 2024 Season 2 on at
    Get URL of Item Picture using custom API.
    I think you can change the idea, convert it first and save it in blob, and then read it from the blob field via API?
     
    Hope this can give you some hints.
    Thanks.
    ZHU
  • Suggested answer
    gdrenteria Profile Picture
    gdrenteria 11,307 Most Valuable Professional on at

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,912 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,355 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans