Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

Get URL of Item Picture using custom API.

Posted on by 47
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.
Categories:
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 69,864 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 9,579 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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 228,552 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans