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>

21 Comments

  1. Excellent! Thanks for looking out for the little guy as always, Marc- this would have helped me a great deal in my beginnings! Though, I still am partial to “‘Hello, Tasha!’” ^_~

    Reply
  2. Hi
    In my application i want to check the Unique Project Name from the list when the user enter the Project name in Project TextBox . And this check should be done for the first time of insertion. Please tell me how can i achieve and i am very new to sharepoint . It could be my pleasure if u help me. And i am not getting how to use $().SPServices.SPRequireUnique.

    Reply
    • Prateek:

      Please post your question in the SPServices Discussions. You should also review the general instructions on the first page of documentation along with the SPRequireUnique doc.

      M.

      Reply
  3. I am rookie about spservices with jquery. I am trying to get [Hyperlink] column from my list and attach that to color coded calculated field. So that Color Coded lights would act as a link which would redirect user to url entered in [Hyperlink] column.
    Can anyone please help me how to start and some logics behind it.

    Thank You

    Reply
  4. Is there any particular placeholder to put the script references? I’ve checked them by Ctrl + Click and they all work but the popup still does not fire. I am trying to implement in MOSS 2007.

    Reply
  5. Can jquery spservices be used from a remote location ? EG Pass caml query from a page which is part of a remote Ruby, ASP, JSP etc.. application

    Can credentials be passed? .

    Reply
    • Richard:

      It’s hard to give a blanket answer for your question. I’ve written SPServices to work within SharePoint, but some of the core functionality *might* work externally, assuming you set the right default option values.

      M.

      Reply
  6. Hi Marc,
    I’m a c# developer, but quite new on SharePoint. My task is to send alert emails if Modified date is older than certain days. I was redirected from codeplex.
    Q1. How can I start with SPServices?
    <> on my local desktop?

    Q2. Should I install SharePoint on my local? or work on SharePoint server?
    <>

    THX.
    David

    Reply
    • David:

      While SPServices could potentially help you with this, it sounds more like something you should do with a workflow or timer job. Client side code requires some sort of trigger based on a user action (which can be as simple as loading a page). For your emails, you’d probably want to check the items automatically every day.

      As for working with SPServices, it’s a script library, so you just need to reference it in your page(s).

      M.

      Reply
      • Thanks Marc, i created a js batch in VS on my local and have empty on the second popup alert. Should i install SharePoint on my local desktop? Or do i have to work on the SP server?
        I just want to create a stand alone app that works as a scheduled task on SP server.
        DVD

        Reply
        • David:

          You seem to be asking the same questions over and over. Again, it really depends on what you want to do. Many people use VMs to develop and the push the WSP to test, then prod. Your governance model figures into all of this, too.

          M.

          Reply
  7. Thanks Marc for your response. Should i work on SharePoint server or install SharePoint on my local? If i create batch within VS, i have empty in the second popup alert. Can i work on my local desktop?
    DVD

    Reply
    • There’s no one answer to your questions. It really depends on what you are building, how you are using SharePoint, what tools you wnt to use, etc.

      M.

      Reply
  8. Hey Marc,

    I tried for some time to get SPServices working, but I couldn’t. Then I remembered the environment I worked in had enabled the NoConflict mode of jQuery by calling jQuery.noConflict(). If you do this, then you can no longer call jQuery using the $-sign. Your SPServices doesn’t seem to be prepared to deal with this. I had to manually edit the SPServices source code and replace all ‘$(‘ with ‘jQuery(‘.

    I don’t know if this is already mentioned somewhere on your site, but I couldn’t find it. And it cost me some hours fixing this issue, even though in hindsight it’s such a simple thing :-)

    Greetings,

    Leon Zandman

    Reply

Leave a Reply