Need Help with SPServices GetListItem

May 24, 2014 at 1:37 AM
Hi Guy's,

I've recently tried to get data from a different site using "getlistitem" operation however I cant seem to get any data from other sites. Although Locally it seems to work just fine.

for Example I am in "SP2007 Site A" and I tried to get data from "SP2007 Site B"
*Site A and Site B are located on 2 different servers.

Also I tried to get data from a "SharePoint 2013 site" using a CEWP on a SP2007 page and was not able to pull data.


I used the ff code:
<script language="javascript" type="text/javascript" src="MySite/JQueryLibraries/jquery-1.7.2.min.js"></script> 
<script language="javascript" type="text/javascript" src="MySite/JQueryLibraries/jquery.SPServices-2013.01.min.js"></script>

<script language="javascript" type="text/javascript">

$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    webURL: "My SP 2013 Site",
    listName: "Announcements",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
      });
    }
  });
});
</script>
<ul id="tasksUL"/>
All my attempts are done using CEWP and I just change the value of the webURL Option.

Does SP GetListItem work from cross server? or did I miss something in the execution?

also if it can work for cross servers can it also work with different versions of SharePoint?

Thank You in Advance!
May 25, 2014 at 2:21 AM
Edited May 25, 2014 at 2:22 AM
I believe that if you are accessing a list in another site collection, you need to use the list's Globally Unique ID (GUID) for the webURL parameter. It will be in the form of {CE01F1D3-0A8A-43D5-A987-BA6071BD12B5}. There are several ways to find a list's GUID, but an easy way is via SharePoint Designer. Just click on the list name in SharePoint Designer, and it will display info regarding the list, including the List ID, which is it's GUID. Be sure to put it within quotation marks when specifying the webURL parameter in the GetListItems function.
May 27, 2014 at 12:24 PM
I've never had trouble getting data from other sites or site collections as long as the permissions are right and the server is configured to accept cross-origin resource sharing requests. I don't have to use GUIDs in that case, either. Try adding the following line at the top of your code:
$.support.cors = true;      //For Cross-Origin Resource Sharing (CORS)
If the server is configured to accept CORS requests, then everything should work fine. Otherwise, you may have some other security architecture issues to deal with.

Geoff
Marked as answer by Francisism on 6/4/2014 at 3:12 AM
Coordinator
May 27, 2014 at 2:09 PM
You can always use the list's name (e.g., Announcements) or the GUID, regardless where it is stored.

webURL must be a path reference. So, for instance, if you are in /sites/siteA and want to read from a list in /sites/SiteB, then the webURL should be "/sites/SiteB". In other words, not the site's name, but the path.

M.
May 28, 2014 at 4:39 PM
Edited May 28, 2014 at 4:41 PM
Thanks everyone!

I'll try your suggestions and give you feedbacks.

Right now I wasn't using relative paths on my URL so my webURLvalues would be using these patterns:

"https://mySP2007Server1.com/sites/SiteA/" and "https://mySP2013Server1.com/SiteB/" and "https://mySP2007Server2.com/sites/SiteC/'

*So far from my previous efforts, I was able to get the data if they reside on the same server, I managed to get data from 2 separate sites as long as they are on the same server ie: "mySP2007Server1.com"

@gkoliver: Should I insert the Script
$.support.cors = true;      //For Cross-Origin Resource Sharing (CORS)
after
$(document).ready(function() {
or after
<script language="javascript" type="text/javascript">
Hopefully It's not access or server configuration thats preventing me from doing so because I dont have priviledge to access anything beyond the Site Collections that we are assigned to.
May 28, 2014 at 6:08 PM
Either location will work for that command as long as it fires before you make your web service call. You only have to fire it once on the page and it's good for the life of the page. When operating within a single site collection, you don't need CORS support (you're not crossing any domain boundaries). When I operate across site collections or across server farms and domains, my script fails every time if I don't have the CORS command in place somewhere before I make my web service call. Keep in mind, though, there may be other security configurations in place at the destination server that may prevent cross-site scripting or may prevent passive authentication. Also, the destination server must be configured to accept cross-origin requests from your origin point. Click here for more info on CORS.
May 31, 2014 at 12:57 AM
Edited May 31, 2014 at 12:59 AM
Hi Everyone,

Thanks again for the help.

After adding "$.support.cors = true" to my script the list was able to pull data from different servers however this seems to only work with SharePoint2007 Sites.

If I try to do get data from an "Announcements" list that I created on our SP2013 Test site and insert and modify the code above inside a SP2007 Site, the code will not work but if I change the WebURL to another SP2007 Site it starts working again.

And if I placed the code within a CEWP on a SP2013 the code will also not work even if I'm pointing to a SP2007 Site it also doesn't work if I point the Web URL to the same 2013 Site by means of removing the weburl declaration or by declaring our SP 2013 as the web url target.
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.01/jquery.SPServices.min.js"></script>

<script language="javascript" type="text/javascript">    
$.support.cors = true;      //For Cross-Origin Resource Sharing (CORS)
$(document).ready(function() { 
  $().SPServices({
    operation: "GetListItems",
    async: false,
    webURL: "Any_SP2007_site",
    listName: "Announcements",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
      });
    }
  });
});
</script>
<ul id="tasksUL"/>
Basically the pattern goes as follows:

Location of CEWP: SP2007SiteA
WebURL Declared: SP2007SiteA
List: Announcements
Results: Success

Location of CEWP: SP2007SiteA
WebURL Declared: SP2007SiteB
List: Announcements
Results: Success

Location of CEWP: SP2007SiteB
WebURL Declared: SP2007SiteA
List: Announcements
Results: Success

Location of CEWP: SP2007SiteB
WebURL Declared: SP2007SiteB
List: Announcements
Results: Success

Location of CEWP: SP2007SiteA
WebURL Declared: SP2013SiteC
List: Announcements
Results: Fail

Location of CEWP: SP2007SiteB
WebURL Declared: SP2013SiteC
List: Announcements
Results: Fail

Location of CEWP: SP2013SiteC
WebURL Declared: SP2007SiteA
List: Announcements
Results: Fail

Location of CEWP: SP2013SiteC
WebURL Declared: SP2007SiteB
List: Announcements
Results: Fail

Location of CEWP: SP2013SiteC
WebURL Declared: SP2013SiteC
List: Announcements
Results: Fail

I've tried various combinations and minor modifications to the codes such as removing the "language = javascript" when placing the code on SP2013 but so far I haven't been able to make it work eventhough I was able to implement "Cascading dropdowns" on our SP2013 Sites.

I might try inserting the codes directly in the page with SharePoint Designer but in Theory inserting the codes via CEWP or embedding them to the page directly with SharePoint Designer should achieve the same results.

Is there something with my code?

Thanks again for all the Advice,
Francis
Coordinator
Jun 2, 2014 at 2:28 PM
Adding $.support.cors = true tells jQuery that CORS is supported, but that doesn't mean that the source server supports it.

Requests from one domain to another run into Same-origin policy issues. If you read up on that, you'll see that it is a security mechanism that is actually a good thing, but we often wish we could get around it.

Bottom line: the issues you are running into are expected.

M.
Jun 3, 2014 at 3:09 PM
Edited Jun 4, 2014 at 11:49 AM
Thanks Marc,

As always, you guys have been very helpfull in nearly everything about SharePoint and occasionally even non SharePoint issues.

When it comes to SharePoint Communities SPServices probably has the best one. (or at least among the top 5)

Thanks to the resource you've provided, I happen to notice another difference between our SP2007 and 2013 sites. Our SP 2013 site uses "https" while SP2007 Sites uses "http".

Does the "https" from the SP2013 servers affect the getlistitem operation even if the list is located within the same site?

Regards,
Francis

Update: I managed to get the SP2013 pull the data using GetListItems operation on Lists that reside within the same server. I used the codes abobe with the exception of the WebURL option.