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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Text output of Blob field in API pages

(8) ShareShare
ReportReport
Posted on by 281
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 
I have the same question (1)
  • Suggested answer
    Dr Gomathi MVP, MCT Community lead, MLE Profile Picture
    395 Super User 2025 Season 2 on at
    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
     
  • Gerardo Rentería García Profile Picture
    25,213 Most Valuable Professional on at

    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
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,284 Super User 2025 Season 2 on at
    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;
  • Suggested answer
    YUN ZHU Profile Picture
    95,331 Super User 2025 Season 2 on at
    Hi, hope the following can give you some hints as well.
     
    Thanks.
    ZHU
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,347 Super User 2025 Season 2 on at
  • Rudylar Profile Picture
    114 on at

    Hi, I tried all different suggested solutions. When I export the Blob multiline content, I almost always get ASCII char such as \u0003 or \u0016 or \n or \u001e. 

    I tried using encode UTF8, UTF16, WindowsText, MSDOS. Same result.

    Any idea ? Thanks

     
  • Suggested answer
    OussamaSabbouh Profile Picture
    5,659 on at
    Hello,
     
    API pages can’t return BLOB fields directly.
    To expose your BLOB as text, add a text variable field on the API page and use Get/Set procedures with InStream / OutStream inside the table extension.
    Pattern:
    On API page OnAfterGetRecord → BlobText := Rec.GetMyBlobText();
    On Validate → Rec.SetMyBlobText(BlobText);
    In the table extension, implement:
    procedure GetMyBlobText(): Text
    procedure SetMyBlobText(NewText: Text)

    Only this wrapper approach works — exposing the BLOB itself will always return empty.
     
    Regards,
    Oussama Sabbouh

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,143

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,694 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,067 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans