Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I rebuild the site (site before). I have on the site two or more Webparts. These CEWP are including the same JavaScript and this the JavaScript is displaying a table via:

document.getElementByID(divID).innerHTML=tableHtml;

This code is in the CEWP (The divID and the typeNumber are different for each webpart the rest is the same):

 <script src="https://www.intranet.i/.../status.js" type="text/javascript"></script>
 <script type="text/javascript">
    var statusObj = new Status(typeNumber, "divID");
    var tableOutput = statusObj.startOutput();
 </script>
 <div id="divID" />

The problem is: When I reload the page sometimes it works and all tables are displayed correctly and sometimes only one table is displayed. I wasn't able to find any regularity.

Edit:

When i run the Debugger i got this Error:

throw Error.create(SP.Res.collectionHasNotBeenInitialized)

SCRIPT5022: Die Sammlung wurde nicht initialisiert. Sie wurde nicht angefordert, oder die Anforderung wurde nicht ausgeführt. Ggf. muss die Anforderung explizit erfolgen. sp.runtime.js?rev=9sKdsC9N6p2BiRk3313M7Q%3D%3D, Line 2 Character 23256

share|improve this question

4 Answers 4

up vote 1 down vote accepted

The Problem is solved. I used REST to get the items. This works just fine for multiple Webparts on the same page.

share|improve this answer

My suggestion is use jQuery and create your table only when the document it ready:

$(document).ready(function () { ... });
share|improve this answer

If you do not want to use jQuery you can use _spBodyOnLoadFunctionNames, but be careful if you use this method you should keep in mind that you need to use ClientId to be sure that your function is unique on the page, because this method does not check how many times do you push your function and can call it several times.

Also will be nice to use ClientId to specify id of your divID, in this case you can use several web parts on the page and don't worry that they will be work incorrect with your js function.

    <script src="https://www.intranet.i/.../status.js" type="text/javascript"></script>
    <script type="text/javascript">
        _spBodyOnLoadFunctionNames.push('createTable_<%= ClienId %>')
        function createTable_<%= ClienId %>(){
             var statusObj = new Status(typeNumber, "divID_<%= ClienId %>");
             var tableOutput = statusObj.startOutput();

             var id = 'divID_<%= ClienId %>'
             document.getElementByID(id).innerHTML=tableHtml;
        }
    </script>
    <div id="divID_<%= ClientID %>" />
share|improve this answer

Both of the above answers are true, but incomplete because there are times where your code could be loaded twice, so what you really want to do is implement SharePoint lazy loading of scripts with Scripts On Demand. Lazy loading of scripts also significantly improves page performance.

For this you register your script with scripts on demand:

RegisterSod("key", "relative encoded path");
RegisterSod("my_script.js", "\u002f_layouts\u002fmy_script.js");

Then let the script manager decide. You put the call to the function in your script inside the external script and call:

SOD.executeFunc(key, functionName, callback)

...in the load function names.

This will ensure that it is loaded once, you can also use:

ExecuteOrDelayUntilScriptLoaded(myCallback, key);

...if you prefer

This will execute immediately if the script has already been loaded.

This method is used more extensively in 2013 and you should really get your head around this structure of calls.

I use these in conjunction with JQuery as apposed to pushing them to the OnLoad as I have my own JQuery libraries that handle global variables across sites and manage my localStorage.

Take a look here for examples:

http://www.ilovesharepoint.com/2010/08/sharepoint-scripts-on-demand-spsod.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.