Skip to main content
Post a question

Notifications

Announcements

No record found.

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

Text output of Blob field in API pages

Like (3) ShareShare
ReportReport
Posted on 30 Jan 2025 02:18:10 by 75
Hello, I have extended a base table to create a custom field of type blob and can input text in that , I created API page for the same table, I am getting proper output of other fields but not getting text output of that particular blob datatype field, Plz suggest me code for this one 
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 8,216 Super User 2025 Season 1 on 03 Feb 2025 at 06:56:21
    Text output of Blob field in API pages
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 77,351 Super User 2025 Season 1 on 03 Feb 2025 at 00:05:12
    Text output of Blob field in API pages
    Hi, hope the following can give you some hints as well.
     
    Thanks.
    ZHU
  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    Teddy Herryanto (Th... 12,925 Super User 2024 Season 1 on 02 Feb 2025 at 19:09:34
    Text output of Blob field in API pages
    BLOB fields are not exposed via APIs. This is because you can have a huge amount of data inside the BLOB.
    You can use Stefano blog post to get the BLOB value.
     
    Or you can create a function to convert that blob to text. You can check the GetWorkDescription procedure on Sales Header for example on how to convert.
     
    procedure GetWorkDescription() WorkDescription: Text
        var
            TypeHelper: Codeunit "Type Helper";
            InStream: InStream;
        begin
            CalcFields("Work Description");
            "Work Description".CreateInStream(InStream, TEXTENCODING::UTF8);
            exit(TypeHelper.TryReadAsTextWithSepAndFieldErrMsg(InStream, TypeHelper.LFSeparator(), FieldName("Work Description")));
        end;
  • gdrenteria Profile Picture
    gdrenteria 14,028 Most Valuable Professional on 02 Feb 2025 at 18:21:09
    Text output of Blob field in API pages

    Hi, good day
    I hope this can help you, and give you some hints.

    Dynamics 365 Business Central: handling BLOB fields on APIs – Stefano Demiliani

    Best Regards
    Gerardo

  • Suggested answer
    Dr Gomathi MVP, MCT Community lead, MLE Profile Picture
    Dr Gomathi MVP, MCT... 198 on 01 Feb 2025 at 16:25:11
    Text output of Blob field in API pages
    I'll help you handle the Blob field data in your Business Central API page. Here's how to properly expose and get text output from a Blob field:
     
    First, define the Blob field in your table extension
    tableextension 50100 "YourTableExt" extends "Your Base Table"
    {
        fields
        {
            field(50100; "Your Blob Field"; Blob)
            {
                Caption = 'Your Blob Field';
            }
        }
    }
     
    Create helper methods to read/write Blob data
     
    codeunit 50100 "Blob Handler"
    {
        procedure BlobToText(BlobField: Blob) ReturnValue: Text
        var
            InStream: InStream;
            Line: Text;
        begin
            if BlobField.HasValue() then
            begin
                BlobField.CreateInStream(InStream, TEXTENCODING::UTF8);
                InStream.ReadText(ReturnValue);
            end;
        end;
    
        procedure TextToBlob(var BlobField: Blob; TextValue: Text)
        var
            OutStream: OutStream;
        begin
            Clear(BlobField);
            BlobField.CreateOutStream(OutStream, TEXTENCODING::UTF8);
            OutStream.WriteText(TextValue);
        end;
    }
     
    In your API page, add a text field that represents the Blob data
     
    page 50100 "Your API Page"
    {
        APIVersion = 'v2.0';
        APIPublisher = 'yourPublisher';
        APIGroup = 'yourGroup';
        EntityName = 'yourEntity';
        EntitySetName = 'yourEntities';
        PageType = API;
        SourceTable = "Your Base Table";
        
        layout
        {
            area(Content)
            {
                field(blobContent; GetBlobContent())
                {
                    Caption = 'Blob Content';
                    ApplicationArea = All;
                }
                // ... other fields
            }
        }
        
        var
            BlobHandler: Codeunit "Blob Handler";
    
        local procedure GetBlobContent(): Text
        begin
            exit(BlobHandler.BlobToText(Rec."Your Blob Field"));
        end;
    
        // If you need to handle incoming data
        trigger OnInsertRecord(BelowxRec: Boolean): Boolean
        begin
            BlobHandler.TextToBlob(Rec."Your Blob Field", blobContent);
        end;
    }

     

    Key Points to Remember:


       
    1. Always use proper encoding (UTF8 recommended)

    2. Handle cases where the Blob might be empty

    3. Consider adding error handling for large text content

    4. Test both reading and writing scenarios thoroughly

     

     
     
    Hope this will work. 
     
    Please comment if you have any queries.
     
    If this helps, kindly mark this as a answer
     

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,234 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,994 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading started