Dynamics 365 Business Central: Importing picture/image from URL

Dynamics 365 Business Central

Hi, Readers.
As you might know, you can import pictures from the camera on your device, or from a local file in Business Central. It’s very convenient.
For example, on the Item Card page.

I saw some discussion last month about whether it was possible to import pictures directly from URLs into Business Central?
So Today I would like to talk about how to do it.

Requirements:
1. Add a new Import From URL button in the Item Card.
2. Create a dialog box for the user to enter the URL of the picture
3. Import the picture in the URL into the Item.

OK, let’s start.

1. Add a new Import From URL button in the Item Card.

2. Create a dialog box for the user to enter the URL of the picture

3. Import the picture in the URL into the Item.

To easily test it, I upload a few sample pictures of BC Items to my blog media.

Test Video:

Source Code: You can download it from GitHub.

pageextension 50101 ItemPictureExt extends "Item Picture"
{
    actions
    {
        addafter(ImportPicture)
        {
            action(ImportPictureFromURL)
            {
                ApplicationArea = All;
                Caption = 'Import From URL';
                Image = Import;
                ToolTip = 'Import a picture file from URL.';

                trigger OnAction()
                var
                    PictureURLDialog: Page "Picture URL Dialog";
                begin
                    PictureURLDialog.SetItemInfo(Rec."No.", Rec.Description);
                    if PictureURLDialog.RunModal() = Action::OK then
                        PictureURLDialog.ImportItemPictureFromURL();
                end;
            }
        }
    }
}
page 50100 "Picture URL Dialog"
{
    PageType = StandardDialog;
    Caption = 'Picture URL Dialog';

    layout
    {
        area(content)
        {
            field(ItemNo; ItemNo)
            {
                ApplicationArea = All;
                Caption = 'Item No.';
                Editable = false;
            }
            field(ItemDesc; ItemDesc)
            {
                ApplicationArea = All;
                Caption = 'Item Description';
                Editable = false;
            }
            field(PictureURL; PictureURL)
            {
                ApplicationArea = All;
                Caption = 'Picture URL';
                ExtendedDatatype = URL;
            }
        }
    }

    var
        ItemNo: Code[20];
        ItemDesc: Text[100];
        PictureURL: Text;

    procedure SetItemInfo(NewItemNo: Code[20]; NewItemDesc: Text[100])
    begin
        ItemNo := NewItemNo;
        ItemDesc := NewItemDesc;
    end;

    procedure ImportItemPictureFromURL()
    var
        Item: Record Item;
        Client: HttpClient;
        Content: HttpContent;
        Response: HttpResponseMessage;
        InStr: InStream;
    begin
        Client.Get(PictureURL, Response);
        Response.Content.ReadAs(InStr);
        if Item.Get(ItemNo) then begin
            Clear(Item.Picture);
            Item.Picture.ImportStream(InStr, 'Demo picture for item ' + Format(Item."No."));
            Item.Modify(true);
        end;
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました