sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

Unable to load JSOM files using SPComponentLoader in SPFX Application Customizer

Open sprider opened this issue 9 years ago • 19 comments

Category

  • [ ] Question

I am trying to load the JSOM files in SharePoint Framework extension using SPComponentLoader but I am getting the below error :

Any idea?

Error: Unknown error when loading module "https://mytenant.sharepoint.com/sites/dev/_layouts/15/init.js"

snip20171012_2

Note: The external reference and require approach works.

sprider avatar Oct 12 '17 17:10 sprider

@waldekmastykarz Any suggestions?

sprider avatar Oct 17 '17 15:10 sprider

Have you tried this: https://github.com/SharePoint/sp-dev-docs/blob/master/docs/spfx/web-parts/guidance/connect-to-sharepoint-using-jsom.md

On Tue, Oct 17, 2017 at 12:19 PM, Joseph Velliah [email protected] wrote:

@waldekmastykarz https://github.com/waldekmastykarz Any suggestions?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/SharePoint/sp-dev-docs/issues/925#issuecomment-337274525, or mute the thread https://github.com/notifications/unsubscribe-auth/ACTRnCin2plmzWhCWBjTEUm44cOqUoTkks5stNOFgaJpZM4P3VWA .

russgove avatar Oct 18 '17 16:10 russgove

I am experiencing the same issue. For a site level Application Customizer, following the Imperative approach from the guidance, only works when viewing pages and not when viewing lists. The external Declarative approach resolves it but it is not ideal.

thomblweed avatar Oct 20 '17 14:10 thomblweed

@sprider Have you tried using the same approach in a web part? Just trying to determine if this has to do with the component type (Application Customizer) or the specific version of the SharePoint Framework.

waldekmastykarz avatar Oct 23 '17 06:10 waldekmastykarz

@waldekmastykarz It is working in WebParts but not with Application Customizer. As @thomblweed mentioned, I am using Declarative approach to resolve this.

sprider avatar Oct 23 '17 13:10 sprider

@sprider the problem with the declarative approach is, that it breaks on classic pages where JSOM is already present. Being able to load it imperatively is required whenever you want your web part to work on both classic and modern pages. Since it however works in web parts and breaks in extensions that only work on modern sites, it would be good to check if there would be any side effects when having a solution with both web part and extension using JSOM.

waldekmastykarz avatar Oct 30 '17 06:10 waldekmastykarz

Sure @waldekmastykarz. Thank you.

sprider avatar Oct 31 '17 13:10 sprider

Hi @waldekmastykarz , I have tried with both: extension and web part in one page and it works correctly using SPComponentLoader, but it does not work in modern pages like Site Contents or list view (but works on custom modern site page), and I'm getting exception: Uncaught (in promise) Error: Unknown error when loading module "/_layouts/15/init.js" at e._requireLoadForDifferentVersion (listview-host-assembly_en-us_215dd0d4594aa258debfba42f5d70c95.js:281) at e._handleRequireJsError (listview-host-assembly_en-us_215dd0d4594aa258debfba42f5d70c95.js:281) at b.errback (listview-host-assembly_en-us_215dd0d4594aa258debfba42f5d70c95.js:281) at require-951f856e.js:1 at require-951f856e.js:1 at each (require-951f856e.js:1) at b.emit (require-951f856e.js:1) at require-951f856e.js:1 at each (require-951f856e.js:1) at c (require-951f856e.js:1)

Saulius-Laurinaitis avatar Nov 02 '17 15:11 Saulius-Laurinaitis

I have actually resolved with very strange fix - adding catch, looks like files are loaded by OOTB modern pages:

     `SPComponentLoader.loadScript('/_layouts/15/init.js', {
        globalExportsName: '$_global_init'
      }).catch((error) => {
        
      }).then((): Promise<{}> => {
        return SPComponentLoader.loadScript('/_layouts/15/MicrosoftAjax.js', {
          globalExportsName: 'Sys'
        });
      }).catch((error) => {
        
      }).then((): Promise<{}> => {
        return SPComponentLoader.loadScript('/_layouts/15/SP.Runtime.js', {
          globalExportsName: 'SP'
        });
      }).catch((error) => {
        
      }).then((): Promise<{}> => {
        return SPComponentLoader.loadScript('/_layouts/15/SP.js', {
          globalExportsName: 'SP'
        });
      }).catch((error) => {
        
      })`

Saulius-Laurinaitis avatar Nov 02 '17 18:11 Saulius-Laurinaitis

I'm getting exactly the same error but for some reason @Saulius-Laurinaitis fix (adding catch) is not working for me. Any other suggestions to fix this issue using the imperative approach? Using the declarative approach is not an option for me because I have more than one environment and needing to update the reference each time that I need to deploy in each of the different environments it's a pain. Thanks!

DaniCorretja avatar Nov 30 '17 15:11 DaniCorretja

Just to reiterate @DaniCorretja, I have tried adding catch also with no luck.

thomblweed avatar Dec 05 '17 10:12 thomblweed

Tthis is what I actually have and I'm only running my code after this promise resolve.

var scriptPromise = new Promise((resolve, reject) => {
            Stars.Const.spScriptPromise = SPComponentLoader.loadScript('/_layouts/15/init.js', {
              globalExportsName: '$_global_init'
            }).catch((error) => {
            }).then((): Promise<{}> => {
              return SPComponentLoader.loadScript('/_layouts/15/MicrosoftAjax.js', {
                globalExportsName: 'Sys'
              });
            }).catch((error) => {
            }).then((): Promise<{}> => {
              return SPComponentLoader.loadScript('/_layouts/15/SP.Runtime.js', {
                globalExportsName: 'SP'
              });
            }).catch((error) => {
            }).then((): Promise<{}> => {
              return SPComponentLoader.loadScript('/_layouts/15/SP.js', {
                globalExportsName: 'SP'
              });
            }).catch((error) => {
            }).then(() => {
              resolve();
            });
          });

Saulius-Laurinaitis avatar Dec 05 '17 10:12 Saulius-Laurinaitis

@waldekmastykarz

Hi, any updates? I'm facing to similar issue.

  1. Deployed extension solution
  2. Run serve nobrowser

Got

2018-02-08_1553

errors in console 404 to get microsoft/sp-loader.js

Please advise

UPDATE It works when use debug query

FIXED I didn't deployed build solution

knalbandianbrightgrove avatar Feb 09 '18 14:02 knalbandianbrightgrove

For resolving the above issue on both classic and modern page , which includes ribbon not editable is .. I have created two functions , one loads taxonomy and other loads other scripts like sp.js and sp.runtime js public loadSPTaxonomy() { return new Promise((resolve, reject) => { SPComponentLoader.loadScript("/_layouts/15/sp.taxonomy.js", { globalExportsName: "SP.Taxonomy" }).catch(error => {}); }); } public loadSPDependencies() { return new Promise((resolve, reject) => { SPComponentLoader.loadScript("/_layouts/15/init.js", { globalExportsName: "$_global_init" }) .catch(error => {}) .then((): Promise<{}> => { return SPComponentLoader.loadScript("/_layouts/15/MicrosoftAjax.js", { globalExportsName: "Sys" }); }) .catch(error => {}) .then((): Promise<{}> => { return SPComponentLoader.loadScript("/_layouts/15/SP.Runtime.js", { globalExportsName: "SP" }); }) .catch(error => {}) .then((): Promise<{}> => { return SPComponentLoader.loadScript("/_layouts/15/SP.js", { globalExportsName: "SP" }); }) .catch(error => {}) .then((): Promise<{}> => { return SPComponentLoader.loadScript("/_layouts/15/sp.taxonomy.js", { globalExportsName: "SP.Taxonomy" }); }) .catch(error => {}) .then(() => { resolve(); }); }); }

Now when calling the function ///Modern Page if (document.getElementById("MSOLayout_InDesignMode") == null) { this.loadSPDependencies().then(() => { this.loadCurrentUserInfo(); }); } else { //Classic Page if (document.getElementById("MSOLayout_InDesignMode")["value"] !== "1") { this.loadSPDependencies().then(() => { ///do other stuff }); } else { this.loadSPTaxonomy().then(() => { //do whatever u wnat }); } } }

Thus depending on type of page and mode of page , the above code works.

But still i noticed one issue in classic page , the files sp.js and sp.runtime js are loaded twice. This will hamper performance. Any thing on this issue ?

harshdamaniahd avatar Apr 24 '18 05:04 harshdamaniahd

Cool find @Saulius-Laurinaitis! Small update to the below, to avoid loading scripts twice in Classic (which can break the page).

  private loadSP() : Promise<any> {
    var globalExportsName = null, p = null;
    var promise = new Promise<any>((resolve, reject) => {
      globalExportsName = '$_global_init'; p = (window[globalExportsName] ? Promise.resolve(window[globalExportsName]) : SPComponentLoader.loadScript('/_layouts/15/init.js', { globalExportsName }));
      p.catch((error) => { })
        .then(($_global_init): Promise<any> => {
          globalExportsName = 'Sys'; p = (window[globalExportsName] ? Promise.resolve(window[globalExportsName]) : SPComponentLoader.loadScript('/_layouts/15/MicrosoftAjax.js', { globalExportsName }));
          return p;
        }).catch((error) => { })
        .then((Sys): Promise<any> => {
          globalExportsName = 'SP'; p = ((window[globalExportsName] && window[globalExportsName].ClientRuntimeContext) ? Promise.resolve(window[globalExportsName]) : SPComponentLoader.loadScript('/_layouts/15/SP.Runtime.js', { globalExportsName }));
          return p;
        }).catch((error) => { })
        .then((SP): Promise<any> => {
          globalExportsName = 'SP'; p = ((window[globalExportsName] && window[globalExportsName].ClientContext) ? Promise.resolve(window[globalExportsName]) : SPComponentLoader.loadScript('/_layouts/15/SP.js', { globalExportsName }));
          return p;
        }).catch((error) => { })
        .then((SP) => {
          resolve(SP);
        })
      ;
    });
    return promise;
  }

jsnl85 avatar Jul 09 '18 13:07 jsnl85

@VesaJuvonen,

Would it make sense to include a {TenantDomainName} token for this to work declaratively with the config.json file? Example included below where {TenantDomainName} would be automatically recognised as the Tenant Domain Name (i.e. https://contoso.sharepoint.com).

"externals": {
  "sp-init":{"path":"{TenantDomainName}/_layouts/15/init.js","globalName":"$_global_init"},
  "microsoft-ajax":{"path":"{TenantDomainName}/_layouts/15/MicrosoftAjax.js","globalName":"Sys","globalDependencies":["sp-init"]},
  "sp-runtime":{"path":"{TenantDomainName}/_layouts/15/SP.Runtime.js","globalName":"SP","globalDependencies":["microsoft-ajax"]},
  "sharepoint":{"path":"{TenantDomainName}/_layouts/15/SP.js","globalName":"SP","globalDependencies":["sp-runtime"]}
}

jsnl85 avatar Jul 10 '18 02:07 jsnl85

Because this issue has been around for a long time & there have been multiple updates to SPFx since then, can you please confirm if it's still an open issue and if so, share some updated details on the SPFx version you've tested it in? I'm unable to repro with latest SPFx (v1.7.1)

andrewconnell avatar Jan 22 '19 15:01 andrewconnell

No issues on 1.7.0 as well

Saulius-Laurinaitis avatar Jan 23 '19 07:01 Saulius-Laurinaitis

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues

msft-github-bot avatar Jan 29 '20 00:01 msft-github-bot