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 :
Microsoft Dynamics NAV (Archived)

GetItemPicture with Codeunit

(0) ShareShare
ReportReport
Posted on by

Hello,

I need to create a code unit method to get item picture.

I only have the SetItemPicture method working.

SetItemPicture(ItemNo : Code[20];Picture : BigText)
Item.GET(ItemNo);

Bytes := Convert.FromBase64String(Picture);
MemoryStream := MemoryStream.MemoryStream(Bytes);

Item.Picture.IMPORTSTREAM(MemoryStream, ItemNo, 'image/png');

Item.MODIFY;

I tried couple of example but they never work with NAV 2017

Thanks,

/Eric

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    I Gusti Made Ari Profile Picture
    3,594 on at
    RE: GetItemPicture with Codeunit

    Hi , please try  below function "GetItemPicture".

    Check below link for the detail :

    https://www.kauffmann.nl/2012/04/04/binary-data-with-nav-web-service/

  • Community Member Profile Picture
    on at
    RE: GetItemPicture with Codeunit

    I cannot use in NAV 2017 Item.Picture.CREATEINSTREAM(IStream);

    I tried that already :(

  • Suggested answer
    Suresh Kulla Profile Picture
    50,237 Super User 2025 Season 2 on at
    RE: GetItemPicture with Codeunit

    Try something like this

    Item.GET(ItemNo);

    Item.CALCFIELDS(Picture);

    IF NOT Item.Picture.HASVALUE THEN

     EXIT;

    FileName := FileMgt.ServerTempFileName('txt');

         FileMgt.BLOBExportToServerFile(TempBodyTempBlob,FileName);

         EXIT(Convert.ToBase64String(File.ReadAllBytes(FileName)));

         FileMgt@1001 : Codeunit 419;

         Convert@1003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";

         File@1004 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.File";

         FileName@1002 : Text;

  • Community Member Profile Picture
    on at
    RE: GetItemPicture with Codeunit

    Thanks for the help, what is TempBodyTempBlob type ?

    I get this error :

    2019_2D00_02_2D00_07_5F00_7_2D00_56_2D00_16.jpg

  • Suggested answer
    I Gusti Made Ari Profile Picture
    3,594 on at
    RE: GetItemPicture with Codeunit

    Hi, you can use below as alternative way.

    Tested is working .

    Name DataType Subtype
    Item Record Item
    Instr InStream  
    MediaRec Record Tenant Media
    MemoryStream DotNet System.IO.MemoryStream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    Bytes DotNet System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    Convert DotNet System.Convert.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    Picture BigText  

    Item.GET('1600');
    IF Item.Picture.COUNT = 0 THEN
        EXIT;

    MediaRec.GET(Item.Picture.ITEM(1));
    MediaRec.CALCFIELDS(Content);
    IF MediaRec.Content.HASVALUE THEN BEGIN
        MediaRec.Content.CREATEINSTREAM(Instr);
        MemoryStream := MemoryStream.MemoryStream();
        COPYSTREAM(MemoryStream,Instr);
        Bytes := MemoryStream.GetBuffer();

         Picture.ADDTEXT(Convert.ToBase64String(Bytes));
    END;

    MESSAGE('%1',Picture);

  • Community Member Profile Picture
    on at
    RE: GetItemPicture with Codeunit

    Thanks now it compile but when I try to run I get that error message 

    <s:Envelope xmlns:s="schemas.xmlsoap.org/.../envelope">
    <s:Body>
    <s:Fault>
    <faultcode xmlns:a="urn:microsoft-dynamics-schemas/error">
    a:Microsoft.Dynamics.Nav.Types.Exceptions.NavNCLMetadataCompileErrorException
    </faultcode>
    <faultstring xml:lang="en-US">
    Compilation of assembly 'C:\ProgramData\Microsoft\Microsoft Dynamics NAV\100\Server\MicrosoftDynamicsNavServer$TEST-TEST\assembly\Codeunit50001_134.dll' failed. This can be caused by differences between binaries in your installation or your database. Ensure that all installation components are consistent and up to date. Error details: c:\ProgramData\Microsoft\Microsoft Dynamics NAV\100\Server\MicrosoftDynamicsNavServer$TEST-TEST\radsource\Codeunit50001_134.cs(383,87) : error CS1656: Cannot assign to 'ALItem' because it is a 'method group' c:\ProgramData\Microsoft\Microsoft Dynamics NAV\100\Server\MicrosoftDynamicsNavServer$TEST-TEST\radsource\Codeunit50001_134.cs(383,65) : error CS1502: The best overloaded method match for 'Microsoft.Dynamics.Nav.Runtime.ALCompiler.ToNavValue(byte)' has some invalid arguments c:\ProgramData\Microsoft\Microsoft Dynamics NAV\100\Server\MicrosoftDynamicsNavServer$TEST-TEST\radsource\Codeunit50001_134.cs(383,87) : error CS1503: Argument 1: cannot convert from 'method group' to 'byte'
    </faultstring>
    <detail>
    <string xmlns="schemas.microsoft.com/.../Serialization">
    Compilation of assembly 'C:\ProgramData\Microsoft\Microsoft Dynamics NAV\100\Server\MicrosoftDynamicsNavServer$TEST-TEST\assembly\Codeunit50001_134.dll' failed. This can be caused by differences between binaries in your installation or your database. Ensure that all installation components are consistent and up to date. Error details: c:\ProgramData\Microsoft\Microsoft Dynamics NAV\100\Server\MicrosoftDynamicsNavServer$TEST-TEST\radsource\Codeunit50001_134.cs(383,87) : error CS1656: Cannot assign to 'ALItem' because it is a 'method group' c:\ProgramData\Microsoft\Microsoft Dynamics NAV\100\Server\MicrosoftDynamicsNavServer$TEST-TEST\radsource\Codeunit50001_134.cs(383,65) : error CS1502: The best overloaded method match for 'Microsoft.Dynamics.Nav.Runtime.ALCompiler.ToNavValue(byte)' has some invalid arguments c:\ProgramData\Microsoft\Microsoft Dynamics NAV\100\Server\MicrosoftDynamicsNavServer$TEST-TEST\radsource\Codeunit50001_134.cs(383,87) : error CS1503: Argument 1: cannot convert from 'method group' to 'byte'
    </string>
    </detail>
    </s:Fault>
    </s:Body>
    </s:Envelope>

  • Community Member Profile Picture
    on at
    RE: GetItemPicture with Codeunit

    Hi,

    After some debugging the error comes from that line

    MediaRec.GET(Item.Picture.ITEM(1)); 

    LOCAL GetItemPicture(ItemNo : Code[20];VAR Picture : BigText)
    Item.GET(ItemNo);
    MediaRec.GET(Item.Picture.ITEM(1)); 
    MediaRec.CALCFIELDS(Content);
    TempBlob.Blob := MediaRec.Content;
    TempBlob.Blob.CREATEINSTREAM(Instr);
    MemoryStream := MemoryStream.MemoryStream();
    COPYSTREAM(MemoryStream,Instr);
    Bytes := MemoryStream.GetBuffer();
    Picture.ADDTEXT(Convert.ToBase64String(Bytes));

  • Suggested answer
    Suresh Kulla Profile Picture
    50,237 Super User 2025 Season 2 on at
    RE: GetItemPicture with Codeunit

    Create a TempBlob of Type Record 99008535

    and assign the Picture to it like below

    after Item.CALCFIELDS(Picture)

    TempBlob.Blob := Item.Picture;

    and then use the TempBlob in the FileMgt.BLOBExport(TempBlob,FileName)

  • Community Member Profile Picture
    on at
    RE: GetItemPicture with Codeunit

    Hi,

    Thanks for your help but I cannot compile the code you send me.

    The error comes from the line 

    TempBlob.Blob := Item.Picture;

    2055.CodeUNit.jpg

    /Eric

  • Suggested answer
    I Gusti Made Ari Profile Picture
    3,594 on at
    RE: GetItemPicture with Codeunit

    Item Picture in NAV 2017 is no longer blob, you should get it from tenant media instead.

    Item.GET(ItemNo);
    IF Item.Picture.COUNT = 0 THEN
        EXIT;

    MediaRec.GET(Item.Picture.ITEM(1));
    MediaRec.CALCFIELDS(Content);
    IF MediaRec.Content.HASVALUE THEN BEGIN

       TempBlob.blob  = MediaRec.Content;

       //continue here

    END;

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

#1
Alexander Ermakov Profile Picture

Alexander Ermakov 2

#2
SC666 Profile Picture

SC666 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans