0

I have 5 different scripts that pull URLs from a list based on a parameter passed to the page: enter image description here

Each of the 5 tabs has almost the same script, each one just pulls from a different column in the list projectparameters: enter image description here

Using the following code:

    <html>
<head>
    <script type="text/javascript">
        ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");

        function retrieveListItems() {
            var parameter = GetUrlKeyValue('ProjectName'); //get the parameter name, tested and it works

            var clientContext = new SP.ClientContext(); //create client context at this site collection.
            var oList = clientContext.get_web().get_lists().getByTitle('ProjectParameters'); //create list object by the title of the, projectParameters is the list to look through
            var camlQuery = new SP.CamlQuery(); //create CAMLquery to pull in list items.
            camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'LinkTitle\'/>' + 
            '<Value Type=\'Computed\'>' + parameter + '</Value></Eq></Where></Query></View>' + 
            '<RowLimit>10</RowLimit></View>'); //define the query, here we pull in the field where the parameter matches. 
            this.collListitem = oList.getItems(camlQuery);//get the items from the list object based on the query defined above.

            clientContext.load(collListitem);//telling the object model to prepare to execute the following object by loading it into the client context.

            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));  
        }
        function onQuerySucceeded(sender, args) {
            var Budget = '';

            var listItemEnumerator = collListitem.getEnumerator();

            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                Budget += oListItem.get_item('Project_x0020_Budget');
            };
            var docUrl= Budget;//set the doclibname to the pulled field

            modIframe(docUrl);
        }

        function onQueryFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
        function modIframe(DocumentLibraryUrl){
            document.getElementById('doclibView').src=DocumentLibraryUrl;
        }

    </script>
</head>
<body>
    <iframe src='' height=600 width=900 id='doclibView'>
    </iframe>
</body>

The issue is that only one of the scripts is running at the right time. Each of the javascript files uses the "executeordelayuntilscriptloaded" method to wait for sp.js to load, but it doesn't seem to be working correctly.

The dev tools show that: enter image description here

The other 4 tabs look like such: enter image description here

Using the dev tools I found that the src field of the iframe used isn't being filled. This is odd considering that is the point of the javascript- to put the correct link in the iframe. I have tried adding

    <script src="http://yoursite/_layouts/1033/init.js"></script>
<script src="http://yoursite/_layouts/MicrosoftAjax.js"></script>
<script src="http://yoursite/_layouts/sp.core.js"></script>
<script src="http://yoursite/_layouts/sp.runtime.js"></script>
<script src="http://yoursite/_layouts/sp.js"></script>

To the beginning of each script but then I get even more errors. I feel like I am missing something really small.

  • try with RegisterSod to register your script, RegisterSodDep to register the dependency – Shawn Oct 5 '16 at 5:50
0

Try this order and make sure all scripts are loaded properly

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://yoursite/_layouts/sp.js"></script>
<script src="http://yoursite/_layouts/sp.core.js"></script>
<script src="http://yoursite/_layouts/sp.runtime.js"></script>
<script src="http://yoursite/_layouts/1033/init.js"></script>
<script src="http://yoursite/_layouts/MicrosoftAjax.js"></script>
0

I ran into something similar. You can't use two functions both with the name OnQuerySucceeded. (If I remember correctly). Try changing the function name on the other scripts OnQuerySucceeded2, etc..

  • i only have one function named OnQuerySucceeded? – Sabien Ambrose Oct 5 '16 at 16:58
  • I'm late to respond here, but you said each of your tabs uses the same script. If the same script is used, and the tabs are all on the same page, you are indeed utilizing the same function. I mention this because I had a project Where I had 3 calls to different lists. I received the same error because the async call couldn't be made using the same function for some reason. (never looked into why). So what I did to fix is I renamed each of the onQuerySucceeded function names (onQuerySucceeded, onQuerySucceeded2, onQuerySucceeded3). Then it worked without the errors. – Casey Govero Oct 19 '16 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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