Adding jQuery+SPServices to a SharePoint Page: Step One, Always
As a follow up to my prior post Adding jQuery to a SharePoint Page: Step One, Always, here’s another step one, always, for when you are using jQuery+SPServices. Even if you have referenced the jQuery library correctly, you still may not be referencing SPServices correctly.
To make sure that SPServices is referenced correctly, add this line:
alert($().SPServices.SPGetCurrentSite());
right after
$(document).ready(function() { alert("jQuery");
The first alert will ensure that your jQuery reference is correct. The second will ensure that your SPServices reference is correct, assuming that you see the name of your current site. If you see “undefined” or some value which makes no sense, odds are your reference to SPServices is wrong.
If you get both alerts, you’re past some large percentage of the issues I help with in the SPServices Discussions. The rule of thumb is relatively simple: scripts which depend on other scripts must be referenced after those dependencies. SPServices requires jQuery, so you reference jQuery first and then SPservices. If your dependencies are more complex, consider using a more sophisticated script loader, like the great LABjs from Kyle Simpson (@getify). You can also, of course, use SharePoint’s script loading logic, though I’ve never found that it offers much benefit for the complexity it adds.
Here’s the full simple step one test. Obviously, you’d replace the references with your own paths and filenames.
<script type="text/javascript" language="javascript" src="my_path/jquery-1.6.1.min.js"></script><script type="text/javascript" language="javascript" src="my_path/jquery.SPServices-0.6.2.min.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { alert("jQuery"); alert($().SPServices.SPGetCurrentSite()); }); </script>
SPServices.currentsite not working, jquery alert is working
Ok.
I am Facing issues in thegrid.addJSONData(JSON.parse(newStr));
JSON.parse(newStr) is not working.
Can you please help me in this.
Abhay:
From the looks of it, your issue has nothing to do with jQuery or SPServices, so I have no idea.
M.
Instead of the full url starting with “http:” my SPGetCurrentSite() returns “/”. What went wrong?
Blaise:
It’s just giving you a relative URL.
M.
Thanks.
And I find it gives a different value in my subsite: “mydomain/subset/”. In those pages, my $().SPServices.SPGetListItemsJson(…) failed because the path to asmx service is incorrect.
So my question is, how to make those SPServices functions work properly in a subsite? Anything I need to pay attention?
Blaise:
I’m sorry. I misunderstood your question. I think the issue you’re having is the same as the one described here. If not, please add a comment on the discussion with more details and I’ll try to help you out. Otherwise, I have it on the list to fix in the next release.
M.
Hi Marc,
Thanks for this tip.
I’ve reference the jquery and put in the alert script which are both working, but the result I get for the second alert is simply a / which quite obviously is not really what I was expecting.
My code is below, any ideas what I’m doing wrong?
$(document).ready(function() {
alert(“jQuery references are good”);
alert($().SPServices.SPGetCurrentSite());
});
Daniel:
If you’re running the code in the root site, then “/” is correct. The function returns the relative link to the site.
M.
Hey Marc,
jQuery script is running fine. But getting issues with SPServices. It shows undefined, even it is referenced correctly. Any suggestions??
Thanks
What version of SPServices are you using? You might want to post your code over on the Codeplex discussions (it won’t make it in here).
M.
I am using SPServices 2014.02.
Ok, let’s move this over to the Codeplex discussions so you can post your code. It might be a typo or something.
M.
Marc
Is there any possibility to connect sharepoint 2013 from sharepoint 2010 using spservices or any webservices to fetach list data from sp 2013 to 2010.
Dileep.
Dileep:
I think you’ve asked basically the same question on my other post at http://sympmarc.com/2015/04/20/moving-from-spservices-to-rest-part-3-json-vs-xml/comment-page-1/#comment-122028, and I’ve answered it there.
M.
Hi Marc,
very good solution, but I have a very strange problem: with Safari (OS X) or Firefox (Windows 7) everything works fine. BUT with IE11 just nothing happens. The scripts do not execute? Do you have any idea? I tried compatibility mode, checked IE Options, … nothing helps. I implemented the scripts within the EditForm.aspx of a normal list.
Thanks in advance,
Simon
@Simon:
Hard to say. Have you added the script references in a CEWP or directly in the page? Different browsers deal with invalid HTML differently. For instance, make sure you’ve closed your script tags properly.
M.