Unable to load JSOM files using SPComponentLoader in SPFX Application Customizer
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"

Note: The external reference and require approach works.
@waldekmastykarz Any suggestions?
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 .
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.
@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 It is working in WebParts but not with Application Customizer. As @thomblweed mentioned, I am using Declarative approach to resolve this.
@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.
Sure @waldekmastykarz. Thank you.
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)
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) => {
})`
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!
Just to reiterate @DaniCorretja, I have tried adding catch also with no luck.
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();
});
});
@waldekmastykarz
Hi, any updates? I'm facing to similar issue.
- Deployed extension solution
- Run serve nobrowser
Got

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
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 ?
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;
}
@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"]}
}
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)
No issues on 1.7.0 as well
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