5

Using the SharePoint COM to update a list item, but my function is failing.

function updateProjectEC(projID, totalEC)
{
    if(totalEC !== 0)
    {
         console.log("projID: " + projID);
         console.log("totalEC: " + totalEC);

         var siteUrl = '/ats/CFPC';
         var clientContext = new SP.ClientContext(siteUrl);

         var oList = clientContext.get_web().get_lists().getByTitle('Projects');

         this.oListItem = oList.getItemById(projID);

         oListItem.set_item('TotalEarnedCost', totalEC);

         oListItem.update();

         clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
}

Code is modeled after the MSDN example:

http://msdn.microsoft.com/en-us/library/hh185011%28v=office.14%29.aspx

Which I've gotten to work before, but here it is failing at:

var clientContext = new SP.ClientContext(siteUrl);

With the error:

SCRIPT445: Object doesn't support this action

I've used this set-up previously, in another function contained in a separate CEWP on the same page even, and it works fine. Not sure why this one is breaking down.

Thanks in advance for any assistance.

3 Answers 3

10

It is not able to find the function in any of the loaded libraries. The method is running before sp.js is loaded. Add ExecuteOrDelayUntilScriptLoaded method before calling your function.

ExecuteOrDelayUntilScriptLoaded(updateProjectEC, "sp.js");

or

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', updateProjectEC);
1
  • 1
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', updateProjectEarnedCost); - This worked. I added it in the body of the script before the initial function call and everything appears to be updating properly. Thank you.
    – Joe
    Commented Oct 8, 2013 at 16:07
0

I had the same issue here, and being an application developer in C++ I forgot that the tab/page instance holds the javascript environment in the same way that an undeleted pointer is held by an application for the lifespan of the application. So if for whatever reason your script fails and bails out, then the local SP namespace objects are now inaccessible even if you do Ctrl+F5 - the only solution is to shut down IE and restart.

When I shut down the IE browser and reloaded the page, sp.js reloaded and I didn't see the errors again. I kept the above delay execution code in my script since it's a safer execution path especially when it's on the corpnet - but it means that it's important to ensure your error handling doesn't abbreviate and crash the javascript engine or else you're hosed.

Hope this helped!!

-2

Try to add the SP.js reference to your page or in $(document).ready

$(document).ready(function() {
 ExecuteOrDelayUntilScriptLoaded(updateProjectEC, "sp.js");
});

I am assuming that you have JQuery referenced in your project

2
  • It seems that in 2013, the ExecuteOrDelayUntilScriptLoaded doesn't quite work. Noted here. Thank you for your assistance however.
    – Joe
    Commented Oct 8, 2013 at 16:10
  • Instead of document.ready you should use _spBodyOnLoadFunctionNames.push, thats the document.ready of sharepoint.
    – Gwny
    Commented Jul 23, 2015 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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