Domanda

the problem is very clear that the call of SP.ClientContext and sp.js isnot done yet. Actually, In my master page there is the code to get those two :

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getData);

and it works very well.

and in another page , i need to get other data using JSOM , so i added the same call , and its always the error :

error SP.ClientContext is undefined

so i tried :

RegisterSodDep('RenderData', 'sp.js');
    RegisterSodDep('RenderData', 'SP.ClientContext');
    SP.SOD.executeFunc('SP.ClientContext', null,GetListOfSitesForCurrentUser);

and even :

ExecuteOrDelayUntilScriptLoaded(GetListOfSitesForCurrentUser, "sp.js")

I can see the problem is that "sp.js" isn't get yet, but how can I resolve this, especialy that the call is done in master page for another function, and i can touch this function never .

È stato utile?

Soluzione

I've had similar issues in the past. The trick has been that you need all of those things.

For some reason sometimes the even main code like sp.js isn't loaded or registered in a site. I have no idea why. So you need to register it.

But it might be and it might even be loaded already so you you need the executeOrDelayUntilScriptLoaded() in case it already is. But this won't load it if it isn't already being loaded.

So you then you need the executeFunc() to load it if it isn't already on the way. But this won't do anything if it is already loaded.

//register the urls for the require files 
SP.SOD.registerSod('sp.js','_layouts/15/sp.js');

// if it isn't already loaded wait until it is loaded, and then do something
SP.SOD.executeOrDelayUntilScriptLoaded(function(){
    // Function calls or code in here.
}, "sp.js"); 

// load the file if it isn't already loaded, otherwise do nothing
SP.SOD.executeFunc("sp.js", null, null);

And, as in my linked example above, you may need to also register dependencies. Since we moved to this pattern we haven't had any timing or loading problems with our code that depends upon the SharePoint files.

Altri suggerimenti

On the list custom form, the following works for me.

 $(document).ready(function () {

    //run the code only after sp.js has been loaded
      SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){

      GetContextuserRoles();

      YourFunctionTwo();    


      });

  });

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top