cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
D365_Eric
Helper V
Helper V

PowerApps Portal: Storing JavaScripts in web files

Hello Community,

 

I would like to get feedback and suggestions if using web files to store JavaScripts, so that we can easily re-apply the same JavaScript files to other web pages on the portal, is the right approach.

 

Basically, instead of putting the JavaScript directly on the web page itself, is the use of web files the right approach to do this? Or is there another suggested approach?

 

Please advise.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
justinburch
Microsoft
Microsoft

Hi @D365_Eric,

This is a personal/project preference - do what works best for your team. As can be identified with my drastic preference difference from @UlrikkeAkerbæk, there is no true standard.

At a high level, I prefer to use Web Files - your code can easily live in source control, team members can use Admin Web Roles to upload the files directly in the Portal without CDS access, you can use liquid tags to control which files to upload and when - e.g. you can add a param for a script name for admins to test functionality, and then add the param to script tags, such as 

{% assign is_admin = user | has_role: 'Administrators' %}
{% if params.useFile and is_admin %}
  <script src="{{ params.fileName }}.js">
{% endif %}

I wrote a small tips document on this a few years ago and decided to host it on my site, if you want to check out a table with my pros & cons.

https://justinburch.com/powerapps-portal-development-tips/where-to-code/

View solution in original post

8 REPLIES 8
UlrikkeAkerbæk
Resolver I
Resolver I

Hey @D365_Eric.

 

I put small global script functions in a global web template, and this web template is included in the website's footer web template. This way I only have one place to manage all the small functions. Even if you need to have three exact copies of the same web template it wouldn't be that hard to maintain if you use this approach across websites. 

 

UlrikkeAkerbæk_0-1600277224114.png

 

Libraries and bigger features are stored in js-files in Azure Blob Storage and added to the website the same way I include bootstrap and font awesome-libraries. 

UlrikkeAkerbæk_1-1600277418378.png

 

In addition I put script on the Entity Forms directly. Everyone on the team are restricted to keep scripts in one of these three locations and it works fine for us. Some small exceptions that are well documented and not business critical - mostly for design purposes and enhancing usability.

 

That's what I do for the most part. Looking forward to see what others do! 

 

Thank you for a great question! 

justinburch
Microsoft
Microsoft

Hi @D365_Eric,

This is a personal/project preference - do what works best for your team. As can be identified with my drastic preference difference from @UlrikkeAkerbæk, there is no true standard.

At a high level, I prefer to use Web Files - your code can easily live in source control, team members can use Admin Web Roles to upload the files directly in the Portal without CDS access, you can use liquid tags to control which files to upload and when - e.g. you can add a param for a script name for admins to test functionality, and then add the param to script tags, such as 

{% assign is_admin = user | has_role: 'Administrators' %}
{% if params.useFile and is_admin %}
  <script src="{{ params.fileName }}.js">
{% endif %}

I wrote a small tips document on this a few years ago and decided to host it on my site, if you want to check out a table with my pros & cons.

https://justinburch.com/powerapps-portal-development-tips/where-to-code/

Thank you @UlrikkeAkerbæk  and @justinburch  for your suggestions above!

Swimburger
New Member

@justinburch , you mentioned we can uploaded JavaScript files, but I am only able to upload css files.
I was able to rename the JavaScript files to the css extension, upload them to the portal, and then change the extension back inside the portal. Obviously this is very unintuitive.
Is there an easier way to upload files in general especially bulk? There's a bunch of font and image files I need to upload.

Hi @Swimburger,

You'll need to unblock the .js extension from the system's attachment blacklist.

Access the Advanced Settings as a System Admin

justinburch_0-1620955835956.png

Navigate to Settings > Admin

image.png

System Settings > General tab > Scroll down to "Set blocked file extensions for attachments" and remove the text for js

image.png

After this, you will be able to upload JS files without issue. Note that this also means JS files can be sent through email and uploaded as attachments in general. The only other workaround I've used is to save the files as .txt, but upload as Web Files with the name/path of ".js". 

 

I hope this helps.

Swimburger
New Member

@justinburch, thank you for the very fast reply.
I made that change but I still can only upload CSS files from the theme section.

Swimburger_0-1621006994802.png

 

I don't see how to upload files any other way.

Ahh.. sorry about that @Swimburger, I haven't worked with the Portal much in the last 7 months or so. This seems to be the Portal Studio tool, and I don't have a portal right now to show steps there, but hopefully these two options help. Apologies in advance, a lot of this is going off of memory.

  1. If you have a user with an "Administrator" Web Role, and appropriately have setup (or kept) the Grant Change Web Page Access Control Rule on the Home page, then logging in with that user should grant you a pop-up in the upper right of the Portal
    1. Click "Child Web File"
    2. Use the Browse button to upload the JS file
    3. Set the name and the Path to the filename (ending with .js)
    4. Note that whichever page you are on during this process will be the Parent Page of the Web File, and will then control the full URL - e.g. on Home, it'll be "https://yourportalhere.powerappsportals.com/file.js", but from Cases (assuming /support/), it'll be "https://yourportalhere.powerappsportals.com/support/file.js"
    5. Also note that, unlike CSS, you need to add <script> tags to utilize JS files in the Portal
  2. Upload the file directly in the "Web Files" Dataverse area, via the Portal Management model-driven app
    1. More information here: Create and manage web files - Power Apps | Microsoft Docs

I hope this helps!

All

We are using Web Files to store our common JS files etc.  but have hit a snag in that we were about to apply a site setting for HTTP/X-Content-Type-Options to now be nosniff as a result of our portal site being PCI Tested.  This then means that the Web Files need to have an explicit MIME Type added otherwise the files are rendered as plain/text and all common JScript functions do not then run in the browser.  Has anyone found a way to set the mime type?

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Super User 2 - 2022 Congratulations 768x460.png

Welcome Super Users

The Super User program for 2022 - Season 2 has kicked off!

Top Solution Authors
Users online (3,206)