web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Get URL of Item Picture using custom API.

(1) ShareShare
ReportReport
Posted on by 163
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.
I have the same question (0)
  • Suggested answer
    Gerardo Rentería García Profile Picture
    25,078 Most Valuable Professional on at
    Get URL of Item Picture using custom API.
  • Suggested answer
    YUN ZHU Profile Picture
    95,307 Super User 2025 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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,377

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 2,696 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,512 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans