Question

I followed Vladim's suggestion in post

and it works great on SP 2013 site but not on a SP 2010 mode site in the same farm. I have used the exact same code that works on the 2013 site but I get the error in console:

SCRIPT5007: Unable to get property 'then' of undefined or null reference

I'm trying to get List Id using the below function and then get a popup to upload document:

function GetListID(listName)
    {
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
            var dfd = $.Deferred();            
            var ctx = new SP.ClientContext();
            var lists = ctx.get_web().get_lists().getByTitle(listName);
            ctx.load(lists);
            //console.log("listname: " + listName);
            ctx.executeQueryAsync(
            function () {
                dfd.resolve(lists);
            },
            function (sender, args) {
                dfd.reject(args);
            });
            return dfd.promise();
        });

    }

I call the above function this way:

function UploadFile(popupurl, listName) {            
        var siteUrl =  makeAbsUrl(L_Menu_BaseUrl); 
        var listId;
        console.log("Site Url: " + siteUrl);

        GetListID(listName).then(
            function (lists) 
            { 
                listId = lists.get_id();
                var path = siteUrl + popupurl + listId + '&IsDlg=1';
                console.log("Final Path: " + path);
                var options = {
                    url: path,
                    dialogReturnValueCallback: myDialogCallback
                };
                SP.UI.ModalDialog.showModalDialog(options);
            }, 
            function (error) { console.log(error.get_message()); });                    

    }

    function myDialogCallback(dialogResult, data) {
        if (dialogResult == SP.UI.DialogResult.OK) {                
            alert('The file was uploaded successfully.');                
            SP.UI.ModalDialog.RefreshPage(dialogResult);
        }
        if (dialogResult == SP.UI.DialogResult.cancel) {
            //nothing
        }

    }
Was it helpful?

Solution

Move the deferred creation outside the SOD.executeFunc method call, like this:

function GetListID(listName)
{
  var dfd = $.Deferred();
  SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
   //the rest of your code 
  }); //this closes the executeFunc call
  return dfd.promise();
}

In your original code, GetListID returns void, and that's why you get "then is undefined".

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top