Search code examples
javascriptsharepointsharepoint-2013spservicescewp

Query SharePoint 2013 List from Another Site Collection


I have successfully been using SPServices to query SharePoint lists on my site collection. Now I have a second site collection for another team that would like to see the data already hosted on my first site collection (no, we can't use the same site collection). I want to create CEWP views by querying the data from the original site collection, but the code doesn't work when I run it from the second site collection. Here's what works on the first site:

$().SPServices({
    operation: 'GetListItems',
    async: false,
    listName: 'Requests',
    CAMLViewFields: "<ViewFields>" +
                        "<FieldRef Name='ID' />" +
                        "<FieldRef Name='Title' />" +
                        "<FieldRef Name='Description' />" +
                        "<FieldRef Name='Assignee' />" +
                    "</ViewFields>",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>" + id + "</Value></Eq></Where></Query>",
    completefunc: function (xData, Status) {
        $(xData.responseXML).SPFilterNode("z:row").each(function() { 
            id = $(this).attr("ows_ID");
            title = $(this).attr("ows_Title");
            description = $(this).attr("ows_Description");
            assignee = $(this).attr("ows_Assignee").split(";#");
            //some more formulas
        });
    }
});

How can I modify this to do the same thing (pull data from my original SharePoint list), but from a different site collection?


Solution

  • This is what I ended up getting to work to pull data on my second site collection from the first. I had to rewrite all the SPServices with ajax:

    $.ajax({
        async: true,
        crossDomain: true,
        url: "http://sitecollectionUrl/_api/Web/Lists/GetByTitle('Requests')/Items$filter=startswith(ID, 'test')&$orderby=ID",
        method: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
            "cache-control": "no-cache",
            "postman-token": "452d273c-96f4-d2a1-bd34-463ab627e4ab"
        },
        success: function (data) {
            $.each(data.d.results, function (index, item) {
                id = item.ID;
                title = item.Title;
                description = item.Description;
                //some more formulas
            });
        },
        complete: function (data) {
            //some more formulas
        }
    });