Include Resources in extensions

Good news!

Resource files can now be included as part of an extension. This new option enables developers to package essential resources, such as images, configuration settings, and text files, directly with their extensions, ensuring that all necessary components are readily accessible and improving overall performance.

To add resources to the extension, the following property must be set in the app.json.

"resourceFolders": [<list of folders that contain resources>]

The folders should be listed relative to the root of your project. For example:

"resourceFolders": ["Resources"]

This will include all files under the folder ‘resources’. The resource folders themselves can contain additional subfolders. For example, you could have the following folder structure:

MyApp
| - Resources/
  | - Images/
    | - MyImage.png
  | - Configuration/
    | - MyConfiguration.json
| - src/
  | - MyCodeunit.codeunit.al

Access to these resources is done through the following method:
NavApp.GetResource("resource name": Text; var instream: Instream; [optional] encoding: TextEncoding) 

The name of a resource is its path from the root of its resource folder. So, for the folder structure above, you can do:

procedure MyProc()
var
    resultStream: Instream;
    jsonText: Text;
begin
    NavApp.GetResource("Images/MyImage.png", resultStream);
    // Do stuff with the blob in the resultStream object

    NavApp.GetResource("Configuration/MyConfiguration.json", resultStream, TextEncoding::UTF8);
    jsonText := resultStream.Read(jsonText);
end

There are also methods that directly retrieve resources as Text or JsonObject types:
NavApp.GetResourceAsText("resource name": Text; [optional] encoding: TextEncoding): Text 
NavApp.GetResourceAsJson("resource name": Text; [optional] encoding: TextEncoding): JsonObject 

The ListResources method is also provided to allow for iteration over resources:
NavApp.ListResources([optional] filter: Text)

The filter value for the method can contain wildcards. If it does not contain wildcards, any resources with the filter text within its name will be returned. For example:

/**
* For the following resource structure:
* Resources/
*     Images/
*         SampleImage1.png
*         SampleImage2.png
*         SampleImage3.jpg
*         TemplateImage.jpg
*     Snippet/
*         SampleSnippet.txt
*         TemplateSnippet.txt
*/

// This will return: ["Images/SampleImage1.png","Images/SampleImage2.png","Images/SampleImage3.jpg", "SampleSnippet.txt"]
NavApp.ListResources("Sample");

// This will return: ["Images/SampleImage1.png","Images/SampleImage2.png"]
NavApp.ListResources("Images/*.png")

Resources can only be accessed within the scope of the app that contains them. This means there is no way to access the resources of another app.

Regards,
Tharanga Chandrasekara

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.