javascriptsharepoint-2010custom-actionargumentexceptionrelative-url

Why is Sharepoint's get_current() method inconsistently available?


With this Javascript:

function upsertPostTravelListItemTravelerInfo1() {
        var clientContext = SP.ClientContext.get_current();
        . . .

...I sometimes get this err (not always, as shown by the data from this method inserting into the list a couple of times (but not the other times)); the err msg, as displayed in the browser console (Chrome Dev Tools) is:

Uncaught TypeError: Cannot read property 'get_current' of undefined

Based on this, I tried this at the top of my Javascript in my *.ascx file:

ExecuteOrDelayUntilScriptLoaded(CustomAction, "sp.js");

...but that heaves up a hairball with the following manifestation of apparent crankiness inscribed upon it: "CustomAction not defined"

What the heck, Ramsey! I can't win for losin', as the old saying goes. How can I get get_current() to consistently execute correctly?

So, I tried this code instead:

function upsertPostTravelListItemTravelerInfo1() {
    var clientContext = new SP.ClientContext(siteUrl);
    . . .

...but got this err msg instead:

Uncaught Sys.ArgumentException: Sys.ArgumentException: Value does not fall within the expected range.
Parameter name: serverRelativeUrl

A slew of "gobbledygook" followed (a dump only a pure D geek could love), including the following two portions that look like they might have some connection: if(!SP.ScriptUtility.isUndefined(window._spPageContextInfo)&&!SP.ScriptUtility.isUndefined(window._spFormDigestRefreshInterval)&&!SP.ScriptUtility.isUndefined(window.UpdateFormDigest)){var c=window._spPageContextInfo,d=c.webServerRelativeUrl,e=window._spFormDigestRefreshInterval;

...and:

if(!a)throw Error.argumentNull("serverRelativeUrl");if(!a.startsWith("/"))throw Error.argument("serverRelativeUrl");

Can anybody make heads or tails of this?


Solution

  • In this line of code...

    ExecuteOrDelayUntilScriptLoaded(CustomAction, "sp.js");

    The CustomAction parameter needs to be the name of a JavaScript function that you want to execute after the SP library has loaded (giving us access to all those delightful objects in the SharePoint client object model).

    You could also forgo passing a named function into ExecuteOrDelayUntilScriptLoaded and just stick an in-line function expression into the first parameter like so:

    ExecuteOrDelayUntilScriptLoaded(function(){
         // do something amazing with the client object model here
    }, "sp.js");
    

    So your final code might resemble this:

    function upsertPostTravelListItemTravelerInfo1() {
        ExecuteOrDelayUntilScriptLoaded(function(){
            var clientContext = SP.ClientContext.get_current();
            . . .
    
        },"SP.JS");
    }