Adding jQuery+SPServices to a SharePoint Page: Step One, Always

alert(

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");

Alert GetCurrentSite()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>

Similar Posts

46 Comments

  1. I am Facing issues in thegrid.addJSONData(JSON.parse(newStr));

    JSON.parse(newStr) is not working.

    Can you please help me in this.

      1. 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?

        1. 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.

  2. 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());
    });

  3. Hey Marc,

    jQuery script is running fine. But getting issues with SPServices. It shows undefined, even it is referenced correctly. Any suggestions??

    Thanks

  4. 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.

  5. 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

    1. @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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.