Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Power Apps Pro Dev & ISV
Unanswered

How to load external libraries and add them to dom following best practice?

(0) ShareShare
ReportReport
Posted on by

Hello everyone

 

We're using the google places api for an address autocomplete pcf control. My current issue is that when I open the form from an overview the onload function of the script is not getting called and if then only in rare cases. If I reload the form the onload function is hit everytime.

 

 

private appendGoogleApiUrlToDom(context: ComponentFramework.Context<IInputs>) {
 if (!document.getElementById('googleapi')) {
 let googleApiKey = context.parameters.googleapikey.raw;
 let scriptUrl = "https://maps.googleapis.com/maps/api/js?libraries=places&language=en&key=" + googleApiKey;

 let scriptNode = document.createElement("script");
 scriptNode.id = "googleapi";
 scriptNode.defer = true;
 scriptNode.setAttribute("type", "text/javascript");
 scriptNode.setAttribute("src", scriptUrl);
 scriptNode.onload = () => {
 debugger
 googleApiLoaded = true;
 context.factory.requestRender();
 }
 document.head.appendChild(scriptNode);
 }
 }

 

 

Is there another possibility to load external javascript libraries like the google api? With the onload function I could workaround the issue of google not being defined but later on the control depends on the googleApiLoaded variable which doesn't get set despite google being available.

Thanks!

  • Diana Birkelbach Profile Picture
    3,072 Most Valuable Professional on at
    Re: How to load external libraries and add them to dom following best practice?

    Hi @Anonymous , 

    I've seen an example in the PCF docs: the Map component: https://docs.microsoft.com/en-us/powerapps/developer/component-framework/sample-controls/map-control?WT.mc_id=BA-MVP-5004107

    Basically it uses an Iframe. 
    Didn't tried this out nether...but maybe it still helps...

     

    Kind regards,

    Diana

  • Community Power Platform Member Profile Picture
    on at
    Re: How to load external libraries and add them to dom following best practice?

    Hi @DianaBirkelbach , thanks for your response. These are third party npm packages which do the script loading dynamically. If these packages don't exist at all how would you load hosted scripts?

  • Diana Birkelbach Profile Picture
    3,072 Most Valuable Professional on at
    Re: How to load external libraries and add them to dom following best practice?

    Hi @Anonymous , 

    Usually you don't inject script tags in PCF, but import the scripts and use them inside index.ts.

    Maybe using something like googleapis npm package (I didn't tried it out): https://www.npmjs.com/package/googleapis

     

    I remember about another thread regarding the google maps. Maybe it helps, since the problem seems to be similar: https://powerusers.microsoft.com/t5/Power-Apps-Pro-Dev-ISV/pcf-1022-Error-Control-source-code-declares-an-internal-module/m-p/458946


    Hope it helps!

    Kind regards,

    Diana

  • Community Power Platform Member Profile Picture
    on at
    Re: How to load external libraries and add them to dom following best practice?

    @HemantG  Please delete. I relied on a variable from init function which doesn't get populated everytime as context parameters are still null especially not populated when opening a record from an overview. It has nothing to do with loading external libraries.


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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Leaderboard > Power Apps - Power Apps Pro Dev & ISV

#1
WarrenBelz Profile Picture

WarrenBelz 87 Most Valuable Professional

#2
mmbr1606 Profile Picture

mmbr1606 71 Super User 2025 Season 1

#3
Michael E. Gernaey Profile Picture

Michael E. Gernaey 67 Super User 2025 Season 1

Overall leaderboard