Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

GetItemPicture with Codeunit

Posted on by Microsoft Employee

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

  • Suggested answer
    I Gusti Made Ari Profile Picture
    I Gusti Made Ari 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;

  • Community Member Profile Picture
    Community Member Microsoft Employee 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
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 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
    Community Member Microsoft Employee 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));

  • Community Member Profile Picture
    Community Member Microsoft Employee 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>

  • Suggested answer
    I Gusti Made Ari Profile Picture
    I Gusti Made Ari 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
    Community Member Microsoft Employee 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
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 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
    Community Member Microsoft Employee on at
    RE: GetItemPicture with Codeunit

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

    I tried that already :(

  • Suggested answer
    I Gusti Made Ari Profile Picture
    I Gusti Made Ari 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/

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans