Trouble with SPSservices and Sharepoint Form

Mar 6, 2014 at 9:55 PM
Hi,

I had jQuery and SPServices on a Sharepoint Form.
But I cannot retreived any informations from CurrentUser or Current Site.

This is my code in the Content Editor of the form :

<script src="/SiteAssets/jquery-1.11.0.min.js" type="text/javascript"></script><script src="/SiteAssets/jquery.SPServices-2014.01.min.js" type="text/javascript"></script><script language="javascript" type="text/javascript">
alert("jQuery loaded? " + (jQuery == undefined ? "NO!!!" : "YES."));
alert("SPServices loaded? " + ($().SPServices == undefined ? "NO!!!" : "YES."));
$(document).ready(function() {
   alert($().SPServices.SPGetCurrentSite());
  });
</script>

When I run the form I received this message box :
jQuery loaded? YES.
SPServices loaded? YES.
/ (This is suppose to be the result of the alert($().SPServices.SPGetCurrentSite());)

Can you help me ?
Thanks !
Mar 7, 2014 at 10:18 PM
If your getting '/' back, that means you are running on the "root" site collection. Is that true?

SPServices tries to first get the current site from javascript variable that sharepoint sets on the page. Those are normally not the full URI, but rather relative to the URL's root. So it the URL was https//mysite.com/default.aspx the current site would be /

/Paul


--
Paul T.

-- Sent from Mobile

Mar 10, 2014 at 2:22 PM
Hi Paul,

Thanks for the answer. But this line was for test only. So I tried a good line and I received an empty box.
This is my code :
<script src="/SiteAssets/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="/SiteAssets/jquery.SPServices-2014.01.min.js" type="text/javascript">
</script><script language="javascript" type="text/javascript">
alert("jQuery loaded? " + (jQuery == undefined ? "NO!!!" : "YES.")); 
alert("SPServices loaded? " + ($().SPServices == undefined ? "NO!!!" : "YES."));
$(document).ready(function() {
       alert($().SPServices.SPGetCurrentUser({fieldName: "Title"}));
});
</script>
Can you told me if I do anything wrong ?
Thanks again.
Jeremie
Coordinator
Mar 10, 2014 at 2:38 PM
Edited Mar 10, 2014 at 2:38 PM
Jeremie:

Try calling SPGetCurrentUser with no options set, like this:
alert($().SPServices.SPGetCurrentUser());
When I use your code above, I get exactly what we'd expect, so the code is correct. There must be something else going on.

The SPGetCurrentUser function simply "scrapes" the values from the _layouts/userdisp.aspx?Force=True page. Try loading that page to make sure that the values you expect are there.

M.
Mar 10, 2014 at 3:18 PM
Hi,

I modified the code and again I have a empty box.
<script src="/SiteAssets/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="/SiteAssets/jquery.SPServices-2014.01.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
alert("jQuery loaded? " + (jQuery == undefined ? "NO!!!" : "YES.")); 
alert("SPServices loaded? " + ($().SPServices == undefined ? "NO!!!" : "YES."));
$(document).ready(function() {
       alert($().SPServices.SPGetCurrentUser());
});
</script>
I tried the page http://intranet/_layouts/15/userdisp.aspx?Force=True and this is worked fine. I view all of my infos.
I dont know why but I have problems with jquery and SPServices from the begin I tried to use it.
Can I do something elese ?

For the record, I used this how-to to configure my jquery : http://www.synergyonline.com/Blog/Lists/Posts/Post.aspx?ID=271

Thanks !

Jeremie
Coordinator
Mar 10, 2014 at 3:33 PM
So are you on SharePoint 2013? Are you running in English or some other language?

M.
Mar 10, 2014 at 4:05 PM
Yes, I'm on Sharepoint 2013 STD.
For the moment, the site is in English but I installed the French Language Pack and my users gonna be use the site in french.

J.
Coordinator
Mar 10, 2014 at 4:12 PM
OK, I'm not sure what the issue is, then.

Can you try jQuery 1.10.x?

M.
Mar 10, 2014 at 4:18 PM
Same result with the 1.10.1
<script src="/SiteAssets/jquery-1.10.1.min.js" type="text/javascript"></script> 
<script src="/SiteAssets/jquery.SPServices-2014.01.min.js" type="text/javascript"></script> 
<script language="javascript" type="text/javascript">
alert("jQuery loaded? " + (jQuery == undefined ? "NO!!!" : "YES.")); 
alert("SPServices loaded? " + ($().SPServices == undefined ? "NO!!!" : "YES."));
$(document).ready(function() {
       alert($().SPServices.SPGetCurrentUser());
});
</script>
Mar 10, 2014 at 4:35 PM
Your initial issue was with SPGetCurrentSite. Is that working ok? What is the value you get back or that?

Looking at your URLs, looks like you testing on a machine that has SP installed on it (ie. localhost). I'm guessing this is cross-domain issue.

Any errors in the console?

When you viewed the userdisp.aspx page, what language was it?

What happens when you execute this in the console:

alert($.get($().SPGetCurrentSite() + "_layouts/userdisp.aspx?Force=True"));

This should return the aspx page markup (HTML).




--
Paul T.

-- Sent from Mobile

Mar 10, 2014 at 4:45 PM
Hi,
  1. With the SPGetCurrentSite, the result was \ because my site was the root.
  2. SP is not installed on my machine. It's the real server and the URL http://intranet was a alias for http://spapp-mtl. I have the same problem with each URL.
  3. The result of http://intranet/_layouts/15/userdisp.aspx?Force=True is in English.
  4. If I tried alert($.get($().SPGetCurrentSite() + "_layouts/userdisp.aspx?Force=True")); , I received nothing
<script src="/SiteAssets/jquery-1.10.1.min.js" type="text/javascript"></script><script src="/SiteAssets/jquery.SPServices-2014.01.min.js" type="text/javascript"></script><script language="javascript" type="text/javascript">
alert("jQuery loaded? " + (jQuery == undefined ? "NO!!!" : "YES.")); 
alert("SPServices loaded? " + ($().SPServices == undefined ? "NO!!!" : "YES."));
alert($.get($().SPGetCurrentSite() + "_layouts/userdisp.aspx?Force=True")); 
$(document).ready(function() {
       alert($().SPServices.SPGetCurrentUser());
});
</script>
Mar 10, 2014 at 7:03 PM
Interesting.

What's the output if this:

alert($().SPGetCurrentSite() + "_layouts/userdisp.aspx?Force=True")

Does the output look like the the URL you used to view the userdisp.aspx page? (Everything must match - including the protocol).

Sorry. I'm running out of ideas.


--
Paul T.

-- Sent from Mobile

Mar 10, 2014 at 7:23 PM
I received nothing. No message box.
I received the message for the jquery loaded and for the SPServices loaded but nothing for the current site.
Coordinator
Mar 10, 2014 at 7:54 PM
There was an error in Paul's script. Try this:
alert($().SPServices.SPGetCurrentSite() + "_layouts/userdisp.aspx?Force=True")
M.
Mar 10, 2014 at 9:05 PM
Sorry. On my phone.


--
Paul T.

-- Sent from Mobile

Mar 10, 2014 at 9:18 PM
Ok I received a message from the line : alert($().SPServices.SPGetCurrentSite() + "_layouts/userdisp.aspx?Force=True")

The message is : /_layouts/userdisp.aspx?Force=True
Thanks !
Coordinator
Mar 10, 2014 at 10:03 PM
OK, so you're getting an empty string back from $().SPServices.SPGetCurrentSite(). Are you sure there are no script errors?

I'm running out of ideas. What's the URL of the page you're doing all this on?

M.
Mar 10, 2014 at 10:59 PM
This is my script :
<script src="/SiteAssets/jquery-1.10.1.min.js" type="text/javascript"></script><script src="/SiteAssets/jquery.SPServices-2014.01.min.js" type="text/javascript"></script><script language="javascript" type="text/javascript">
alert("jQuery loaded? " + (jQuery == undefined ? "NO!!!" : "YES.")); 
alert("SPServices loaded? " + ($().SPServices == undefined ? "NO!!!" : "YES."));
alert($().SPServices.SPGetCurrentSite() + "_layouts/userdisp.aspx?Force=True");
$(document).ready(function() {
       alert($().SPServices.SPGetCurrentUser());
});
</script>
The script had a / to _layouts/userdisp.aspx?Force=True. My site is root site collection. Can SPServices work on the root site ?
Thanks
Coordinator
Mar 11, 2014 at 1:34 AM
Yes, SPServices can work in any site. But what is the URL of the page where you are making the call? That's the only thing I can think of that might be unique.

M.
Mar 11, 2014 at 3:25 AM
Also: can you ensure the browser console is opened and then hit refresh. Check if any errors are logged. In IE if the console is not opened before the page is loaded, you may not see errors.


Marc,
I was thinking the same thing. This why I asked if he was running from "localhost". In an earlier post he posted this:


I'm assuming "intranet" is either a masked value when he posted here in the forums, or a real network alias. I would hope this is not a problem - browser should still treat intranet as the host:port portion of the URL.




--
Paul T.

-- Sent from Mobile

Mar 11, 2014 at 5:56 AM
The URL for the page I making the call is : http://intranet/Lists/Demande%20de%20formation/Form_DemandeFormation.aspx?Source=http%3A%2F%2Fintranet%2FLists%2FDemande%2520de%2520formation%2FAllItems%2Easpx&RootFolder=

The error I saw with Firebug is :
http://_layouts/userdisp.aspx?Force=True&1394512968687 Aborted
Accept /
Accept-Encoding gzip, deflate
Accept-Language fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Connection keep-alive
Host _layouts
Origin http://intranet Referer http://intranet/Lists/Demande%20de%20formation/Form_DemandeFormation.aspx?Source=http%3A%2F%2Fintranet%2FLists%2FDemande%2520de%2520formation%2FAllItems%2Easpx&RootFolder=
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0

The errors I saw with IE console :
SEC7118: XMLHttpRequest for http://_layouts/userdisp.aspx?Force=True&1394513463147 required Cross Origin Resource Sharing (CORS).
File: Form_DemandeFormation.aspx
SCRIPT7002: XMLHttpRequest: Network Error 0x2ee7, Could not complete the operation due to error 00002ee7.
File: Form_DemandeFormation.aspx
Mar 11, 2014 at 2:32 PM
Hi guys,

I did some test and I found SPService worked only on subsite not on the root site.

This is the tests I did to have this conclusion :
  1. Create a new custom list in the root site with no field except Title. Add the script. Result = Same error.
  2. Create a subsite and add a new custom list with no field except Title. Add the script. Result = That's worked.
So did you have solution to make the script work in the root site ?

Thanks !
Coordinator
Mar 11, 2014 at 7:14 PM
SPServices absolutely ought to work in the root site.

It looks like you're not getting a value back from $().SPServices.SPGetCurrentSite(). Can you step through the function to see where the issue is?

M.
Mar 11, 2014 at 7:23 PM
What dis mean by strp through the function.
Because I tried a lot of thing and nothing worked on the root site. But I create a subsite and everything I want work great.
Coordinator
Mar 11, 2014 at 7:36 PM
I mean stepping through the script in Firebug, Chrome's development tools, or IE's developer tools (F12).

M.
Mar 11, 2014 at 7:38 PM
I send you the result in previous post :
The error I saw with Firebug is :
http://_layouts/userdisp.aspx?Force=True&1394512968687 Aborted
Accept /
Accept-Encoding gzip, deflate
Accept-Language fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Connection keep-alive
Host _layouts
Origin http://intranet Referer http://intranet/Lists/Demande%20de%20formation/Form_DemandeFormation.aspx?Source=http%3A%2F%2Fintranet%2FLists%2FDemande%2520de%2520formation%2FAllItems%2Easpx&RootFolder=
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0

The errors I saw with IE console :
SEC7118: XMLHttpRequest for http://_layouts/userdisp.aspx?Force=True&1394513463147 required Cross Origin Resource Sharing (CORS).
File: Form_DemandeFormation.aspx
SCRIPT7002: XMLHttpRequest: Network Error 0x2ee7, Could not complete the operation due to error 00002ee7.
File: Form_DemandeFormation.aspx

Is not that you want ?
Coordinator
Mar 11, 2014 at 7:47 PM
Edited Mar 11, 2014 at 7:47 PM
No, I was hoping you could step through the script to see where the error is occurring. The messages above just tell me that the $().SPServices.SPGetCurrentSite() function isn't passing back a value. (The hostname should be present in the links, like http://[hostname]/_layouts/userdisp.aspx?Force=True) I'm wondering why that is happening.

M.
Mar 12, 2014 at 1:52 AM
Marc,
I'm not at a computer. But: I remember that SPGetCurrentSite uses SP available variables to determine the "web root URL" if those are set before using the webservice on the current URL. I'm thinking that property is NOT undefined, but also not set (blank string). Maybe have the user alert() that value?
Also, if you look at the code, do you see any risks on how you using the root site services to use the service? Remember: the URL here looks like: http://aliasName/path/

I will try to look at the code later if possible.


--
Paul T.

-- Sent from Mobile

Coordinator
Mar 12, 2014 at 4:43 AM
I can run $().SPServices.SPGetCurrentSite() from my root site on Office365 and get "/" which is the valid value.

Yes, there were problems in 2013.02, but I thought I had fixed them in 2014.01 (at least I believed I had).

I've spent some time stepping through $().SPServices.SPGetCurrentUser() and I see the problem. It's only going to occur in the root site, as lauzierj is seeing. It's in line 2372 of jquery.SPServices-2014.01.js.
url: thisWeb + "/_layouts/userdisp.aspx?Force=True&" + new Date().getTime()
The value of url will be something like this:

"//_layouts/userdisp.aspx?Force=True&1394595490384"

which is invalid.

Sorry for the trouble with this, lauzierj. A quick fix for you would be to replace line 2372 with this:
url: ((thisWeb === "/") ? "" : thisWeb) + "/_layouts/userdisp.aspx?Force=True&" + new Date().getTime(),
Note that you'd do this in the unminified version of SPServices.

M.
Coordinator
Mar 12, 2014 at 4:54 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Mar 12, 2014 at 1:40 PM
Nice forensic work Marc :)

I also encountered this issue recently when I created my own method for getting the current webURL.

Wouldn't this issue impact all SPServices calls (from the root site)?


--
Paul T.

-- Sent from Mobile

Coordinator
Mar 12, 2014 at 2:29 PM
No, I think it's limited to $().SPServices.SPGetCurrentUser(). It's got its own logic for determining the right path to the userdisp.aspx page. At least that's my hope.

M.
Mar 12, 2014 at 2:33 PM
Hi,

Thanks for your time to help me with that. I gonna keep this information.
For your information, my need was to autofield some fields of a form.
So I create a subsite only for form and I called the script from that and everything work good now.
Thanks for this. It's realy helpfull... when it's work... ;)
Coordinator
Oct 16, 2014 at 2:46 PM
I've just uploaded a new alpha (2014.02ALPHA5) with a fix for this issue and a couple other things. If you could try it out and let me know if it works for you, that would be very helpful.
https://spservices.codeplex.com/releases/view/119578

M.
Nov 3, 2014 at 12:59 PM
Same problem here. SPGetCurrentUser() not functional on root site (2013.01) ... However I tried the last alpha7 and it works. Thanks for your gr8 work!
Coordinator
Nov 3, 2014 at 5:04 PM
Great to hear it works for you, too. That's a nasty bug and you were just seeing one symptom of it. Mea culpa.

I should have the new version out soon.

M.