Client-side web parts using SharePoint libraries in classic pages renders the page unusable
Category
- [ ] Question
- [ ] Typo
- [X] Bug
- [ ] Additional article idea
Expected or Desired Behavior
When creating a client-side Web Part and utilizing JSOM/native SharePoint JavaScripts, following the Connect to SharePoint using the JavaScript Object Model (JSOM) instruction I expect that the client-side Web Part should work on both Modern and Classic pages.
Observed Behavior
When adding the client-side Web Part on a classic page the web part renders somewhat then the page immediately becomes unusable. You can not use the save button or any other ribbon functionality. If you reload the page, the web part is not visible and you cannot still click on edit or the Ribbon. You have to go in to ...pagename?contents=1 and remove the web part to restore the pages functionality.
The JavaScript console also shows the following error:
Error: SP.Runtime.js is already loaded SP.Runtime.js:2
(anonymous) @ SP.Runtime.js:2
You can see that SP.Runtime.js is loaded from both https://contoso.sharepoint.com/_layouts/15/SP.Runtime.js as well as https://static.sharepointonline.com/bld/_layouts/15/16.x.y.z/SP.runtime.js - so there's likely a collision somewhere not handled properly.
(This also has to do with that we cannot use static.sharepointonline.com in our config.js, due to that the version number changes all the time)
Steps to Reproduce
Follow the instructions as described in the referenced SPFx guidance above and add the client-side web part to a classic team site
This issue is also related to mine, I can also confirm that JSOM is intermittently working on /_layouts/15/workbench.aspx and doesn't work at all within modern site page. https://github.com/SharePoint/sp-dev-docs/issues/585
@wictorwilen
On classic webpart however it should be working by default since you can do a pre-flight check for any window.SP exists, then reuse the existing window.SP object, else call the loadSPDependencies method that contains SPComponentLoader promise chains to call JSOM(which stops working on modern pages for now).

@jonathanhotono - yes, that is a work around, but not sustainable long term imo
@wictorwilen Which bit is not sustainable in long term? I reckon it's probably best to reuse existing SP.js in classic page instead of re-calling it again via externals if the webpart must work in classic page.
@jonathanhotono - that's what I mean, then it should not be supported to use SP libraries in "externals" - also manually adding these gives a lot of complexity to your web part.
@wictorwilen do you know if this has been fixed yet? thanks.
I am still receiving this error
public loadSPDependencies() { return new Promise((resolve,reject)=>{ SPComponentLoader.loadScript('/_layouts/15/init.js', { globalExportsName: '$_global_init' }) .then((): Promise<{}> => { return SPComponentLoader.loadScript('/_layouts/15/MicrosoftAjax.js', { globalExportsName: 'Sys' }); }) .then((): Promise<{}> => { return SPComponentLoader.loadScript('/_layouts/15/SP.Runtime.js', { globalExportsName: 'SP' }); }) .then((): Promise<{}> => { return SPComponentLoader.loadScript('/_layouts/15/SP.js', { globalExportsName: 'SP' }); }) .then((): Promise<{}> => { return SPComponentLoader.loadScript('/_layouts/15/sp.taxonomy.js', { globalExportsName: 'SP.Taxonomy' }); }).then(()=>{ resolve(); }) })
}
public componentDidMount() { debugger; this.loadSPDependencies().then(()=>{ this.loadCurrentUserInfo(); }) } What is going wrong..becoz when i add webpart on classic page..the ribbon becomes uneditable
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 ?
Any fix for this? I have a SPFx webpart that needs to work both in classic and in modern sites but on classic sites I'm getting an intermittent error (Error: SP.Runtime.js is already loaded) which makes the ribbon unusable and the page not editable. I'm using the SPComponentLoader to load the scripts.
This issue is being closed as part of an issue list cleanup project. Issues with no activity in the past 6 months that aren't tracked by engineering as bugs were closed as part of this inititive. If this is still an issue, please follow the steps outlined to re-open the issue.