1

I am trying to write a JS function that sends a POST request to a SharePoint List to add new List items to that List. I have done this in the past using SP.ClientContext and would like to continue to do so, as this is the only way I know of to send multiple insert requests in a batch. Here is my code:

/**
 * Inserts new items to a List
 * 
 * @params string siteUrl
 * @params object[] newListItems
 * @params string listName
 * @params string[] listItemFields
 * 
 * @returns bool
**/
function insertListItems(siteUrl, newListItems, listName, listItemFields) {  
    if(newListItems.length == 0) {
        return false;
    }

    var clientContext = new SP.ClientContext.get_current();
    var olist = clientContext.get_web().get_lists().getByTitle(listName); 

    // Get SP list   
    var itemCreateInfo = new SP.ListItemCreationInformation();

    for(var i = 0; i < newListItems.length; i++) {
        oListItem = olist.addItem(itemCreateInfo);

        for(var j = 0; j < listItemFields.length; j++) {
            oListItem.set_item(listItemFields[j], newListItems[i][listItemFields[j]]);
        }

        oListItem.update();
    }

    clientContext.load(oListItem); 
    clientContext.executeQueryAsync(
        this.successHandler("Items inserted to " + listName), 
        this.errorHandler()
    );  
}

I have moved this code to a new SP site, but now when I attempt to run the function I get "jQuery.Deferred exception: 'SP' is undefined ReferenceError: 'SP' is undefined" in my console. One potential issue is that this code was originally written in a SP site where the List was within the same site. Now I am working in a subsite of the SP site and the List is in a different subsite of the same SP site (not my call :/). Another potential issue is that before this was all added to a script tag within html that was embedded in the site. Now I have moved the JS out of that html file and am referring to it with an external script tag, though none of the other scripts I am using in the html file have changed.

B. Allred
  • 419
  • 5
  • 18

2 Answers2

0

Have you tried to refer SP.js explicitly in your html file before you are referencing this script file? if no, trying doing that. if yes, try using ExecuteOrDelayUntilScriptLoaded to let SharePoint load SP.js before function is executed:

jQuery(document).ready(function() {
 ExecuteOrDelayUntilScriptLoaded(myfunction, "sp.js");
});
function myfunction() {
// your code goes here
}

and if list is in a different subsite, you should get context to that particular site using it's URL to get it's list/libraries etc.

var URL = [site url]
var clientContext = new SP.ClientContext(URL);

Please see if this helps.

EDIT

Please check this answer which provides a number of options that you could try, to load needed scripts before your function call: https://stackoverflow.com/a/17326405/2836750

Muhammad Murad Haider
  • 1,357
  • 2
  • 18
  • 34
  • I've actually recently tried this and it seems to be the solution, since multiple sources have recommended it. However, I'm now running into an issue where I'm getting an error that says "ExecuteOrDelayUntilScriptLoaded is not defined". Do you know what the source of this function is and how I can get my html to recognize it? – B. Allred Dec 16 '19 at 19:11
  • Actually SharePoint loads a lot of Javascripts on page load, so there could be a conflict or something that might be causing the problem. would be helpful to see which js files you are referencing. However, you could try '_spBodyOnLoadFunctionNames.push("YourFunction"); i am editing my answer to provide a helpful link. – Muhammad Murad Haider Dec 17 '19 at 04:58
  • The only js references currently in my code are "//ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" and a reference to a file containing the functions I wrote, which does not contain any references to other files. – B. Allred Dec 17 '19 at 15:08
  • Ok, try referencing sp.js explicitly in your html file before you are referencing your script file. Have you tried using '_spBodyOnLoadFunctionNames'? – Muhammad Murad Haider Dec 18 '19 at 06:02
  • `ExecuteOrDelayUntilBodyLoaded(delayBody); function delayBody () { // run stuff here }` worked for me. https://www.c-sharpcorner.com/article/sequence-of-javascript-function-loaded-after-the-ui-has-been-loaded-in-sharepoin/ - `_spBodyOnLoadFunctionNames` has given others problems, too... https://sharepoint.stackexchange.com/questions/73669/spbodyonloadfunctionnames-not-always-working – vapcguy Oct 18 '21 at 02:44
0

Likely tied to sp.js not being loaded. Try doing it explicitly, and you'll need this group of libraries:

<script type="text/javascript" src="/_layouts/15/init.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.core.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>

Then you want to load it:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { });

Normally I would say to also delay your function being called until these loads have happened:

ExecuteOrDelayUntilBodyLoaded(insertListItems); 
function insertListItems() { 
    // run your stuff here 
}

But I don't think you are inserting items into a list onload. Normally you'd be having someone enter data, and then insert it into the list on a button click routine. Should have indicated this and when the function would be called. But I think the first two code blocks would resolve the stated issue.

vapcguy
  • 7,097
  • 1
  • 56
  • 52