How to send item picture to SOAP web service?

Hi experts,

I need to create a SOAP web service codeunit that gets an item number and returns the picture that is on that item in Business Central cloud.

(I think there can be more that one picture, but I only want to get the first picture.)

I have made the procedure below on a codeunit, but it doesn't return anything.

What am I doing wrong?

procedure GetItemImage(itemNo: Code[20]) Result: Text

var
Item: Record Item;
TenantMedia: Record "Tenant Media";
TempBlob: Codeunit "Temp Blob";
PictureInStream: InStream;
PictureOutStream: OutStream;
begin
if Item.Get(itemNo) then begin
if Item.Picture.Count = 0 then begin
exit('');
end;

TenantMedia.Get(Item.Picture.Item(1));
TenantMedia.CalcFields(Content);
if TenantMedia.Content.HasValue then begin
Result := '';
Clear(PictureInStream);
TenantMedia.Content.CreateInStream(PictureInStream);
TempBlob.CreateOutStream(PictureOutStream);
CopyStream(PictureOutStream, PictureInStream);

PictureInStream.ReadText(Result);
end;
end;
end;

Best Answer

  • ftorneroftornero Member Posts: 442
    edited 2021-12-08 Accepted Answer
    Hello @MortenSteengaard

    You can use this function
        procedure GetItemImage(itemNo: Code[20]) Result: Text
    
        var
            Item: Record Item;
            TenantMedia: Record "Tenant Media";
            TempBlob: Codeunit "Temp Blob";
            PictureInStream: InStream;
            base64cnv: Codeunit "Base64 Convert";
            
        begin
            if Item.Get(itemNo) then begin
                if Item.Picture.Count = 0 then
                    exit('');
    
                if TenantMedia.Get(Item.Picture.Item(1)) then begin
                    TenantMedia.CalcFields(Content);
                    if TenantMedia.Content.HasValue then begin
                        TenantMedia.Content.CreateInStream(PictureInStream);
                        Result := base64cnv.ToBase64(PictureInStream, true);
                    end;
                end;
            end;
        end;
    

    Regards

Answers

Sign In or Register to comment.