SPServices only working in Edit Mode

Mar 27, 2014 at 2:34 PM
Edited Mar 27, 2014 at 2:36 PM
Hi there,

so I developed some custom functionality with SPServices that works fine in my test-environment. Now Im trying to deploy it in the productive environment and Ive struggled for several hours but cant get it to work.

I insert the code inside of a simple form webpart.
When the webpart / website is in edit mode, the code executes fine.
When its not in edit mode, the jquery functions work, but the spservices functions do not seem to work. I get the error "Das Objekt unterstützt diese Eigenschaft oder Methode nicht." (in english its something like "The object does not support this property or method."). Line number of the error then points to the first occurrence of $.SPServices().

The only thing thats different in the production environment is, that it uses another masterpage and that I only can use Internet Explorer 8 there (do not ask why...).

I searched nearly the whole Internet and tried different solutions.
I wrapped the $(document).ready(function() {}) like this: ExecuteOrDelayUntilScriptLoaded(function() {}, "sp.js");

And I tried writing the code directly to the webpart page via sharepoint designer. It does not work then either and I even cant enter edit mode.

The references to the scripts seem to work (I can click them in sharepoint designer). I just dont know why it is not working. If you have any further suggestions or have any idea how to tear this problem down, or find out whats maybe causing it, please let me know because Im really clueless right now...

Thank you very much!
Mar 27, 2014 at 6:06 PM
Can you post your code? Obviously your two environments are different, so "test" isn't much of a "test".

If you look at the bottom of the first page of documentation, you'll see a few tricks to make sure that jQuery and SPServices are loading. That may be the issue.

Mar 28, 2014 at 7:05 AM

thank you very much for your answer.
Please see my code here: http://pastebin.com/DvStqiet

The test environments indeed isnt much the same as the productive environment. I was wrong at this point, but I just know these environments since a week, so please excuse the mistake.

I just tried to make sure that jQuery and SPServices are loading, when the webpart is not in edit mode. But Im not really sure how to do this? What do you mean by "If you look at the bottom of the first page of documentation, you'll see a few tricks to make sure that jQuery and SPServices are loading." Do you mean the Debug-Mode for SPServices-Calls?
I think the problem is indeed, that the librarys do not load, when not in Edit mode...

Again, thank you very much for your help! I hope we can figure this out :)
Mar 28, 2014 at 7:59 AM
Edited Mar 28, 2014 at 11:39 AM
OK I maybe found the problem.

The Master-Template of the Productive Environment is already including jQuery 1.4.2...
Now I just need to figure out, how to fix this, because my script does not seem to work with jQuery 1.4.2.
Can I force any kind of "unload" and then load my jQuery 1.11.0?

//Hmm, even if I remove the jQuery instance in my script, so that only the 1.4.2 from the master-template should get loaded, it only works in edit mode... strange.

// I also tried referencing the scripts with absolute URLs. But it still does not work.
Then I tried debugging with Firefox, Console shows the following: http://imgur.com/QvozF38
Mar 31, 2014 at 6:39 AM
Edited Mar 31, 2014 at 6:40 AM
OK it seems like I got it to work with the following:

I just wrapped my entire code inside of
(function($) {
    // Your usual jQuery code here using `$`,
    // this code runs immediately
and now it works. Im really happy, that it finally works... Seems like some other javascript (maybe referenced by the master template?) is overwriting the $.
Mar 31, 2014 at 12:44 PM
What you've done is basically create your own namespace, defining $ == jQuery inside it. You may have been colliding with variables from other scripts in the page.

Glad you got it working!