1

Environment:

  • SharePoint 2010
  • jquery-1.12.3.min
  • jquery.SPServices-2014.02.min

Trying to use SPSerivces method to read a SP list into javascript.

When reviewing the final output I have the following information (key XX has Value YY), which does not relate to the data that I am trying to obtain. This list does not even conform to the return that is detailed within the online documentation.

key state has value function(){return c}
key always has value function(){return e.done(arguments).fail(arguments),this}
key then has value function(){var a=arguments;return n.Deferred(function(c){n.each(b,function(b,f){var g=n.isFunction(a[b])&&a[b];e[f[1]](function(){var a=g&&g.apply(this,arguments);a&&n.isFunction(a.promise)?a.promise().progress(c.notify).done(c.resolve).fail(c.reject):c[f[0]+"With"](this===d?c.promise():this,g?[a]:arguments)})}),a=null}).promise()}
key promise has value function(a){return null!=a?n.extend(a,d):d}
key pipe has value function(){var a=arguments;return n.Deferred(function(c){n.each(b,function(b,f){var g=n.isFunction(a[b])&&a[b];e[f[1]](function(){var a=g&&g.apply(this,arguments);a&&n.isFunction(a.promise)?a.promise().progress(c.notify).done(c.resolve).fail(c.reject):c[f[0]+"With"](this===d?c.promise():this,g?[a]:arguments)})}),a=null}).promise()}
key done has value function(){return f&&(c&&!b&&(h=f.length-1,g.push(c)),function d(b){n.each(b,function(b,c){n.isFunction(c)?a.unique&&j.has(c)||f.push(c):c&&c.length&&"string"!==n.type(c)&&d(c)})}(arguments),c&&!b&&i()),this}
key fail has value function(){return f&&(c&&!b&&(h=f.length-1,g.push(c)),function d(b){n.each(b,function(b,c){n.isFunction(c)?a.unique&&j.has(c)||f.push(c):c&&c.length&&"string"!==n.type(c)&&d(c)})}(arguments),c&&!b&&i()),this}
key progress has value function(){return f&&(c&&!b&&(h=f.length-1,g.push(c)),function d(b){n.each(b,function(b,c){n.isFunction(c)?a.unique&&j.has(c)||f.push(c):c&&c.length&&"string"!==n.type(c)&&d(c)})}(arguments),c&&!b&&i()),this}

What am I doing wrong with this call? How can I store a SP list into a java script variable?

Below is the SPSerivce call and a snippet of data:

function getListUsingSPSerivces(){
  //SPServices Example
  //The Web Service method we are calling, to read list items we use 'GetListItems'
 var method = "GetListItems";  //IGNORE

  //The display name of the list we are reading data from
  var url = "BASE_URL/sp/XXYY/_vti_bin/listdata.svc/"
  var list = "RFCExtract";
  //Acquire GUID
  //var listGUID = $().SPServices.SPListNameFromUrl();
  //console.log (listGUID);
  //list = listGUID;

  var fieldsToRead =     "<ViewFields>" +
                         "<FieldRef Name='RFC_x0020_Number' />" +
                         "<FieldRef Name='Title' />" +
                         "<FieldRef Name='Status' />" +
                         "<FieldRef Name='Date' />" +
                         "<FieldRef Name='Class' />" +
                         "</ViewFields>";

        //this is that wonderful CAML query I was talking about earlier. This simple query returns
        //ALL rows by saying "give me all the rows where the ID field is not equal to 0". I then 
        //tell the query to sort the rows by the Title field. FYI: a blank query ALSO returns
        //all rows, but I like to use the below query because it helps me know that I MEANT to 
        //return all the rows and didn't just forget to write a query :)
        var query = "<Query>" +
                        "<Where>" +
                            "<Neq>" +
                                "<FieldRef Name='RFC_x0020_Number'/><Value Type='Number'>0</Value>" + 
                            "</Neq>" +
                        "</Where>" +
                        "<OrderBy>" + 
                            "<FieldRef Name='RFC_x0020_Number'/>" +
                        "</OrderBy>" +
                    "</Query>";
     //Here is our SPServices Call where we pass in the variables that we set above
  console.log("SPSerivces example");
  //console.log(method);
  console.log(url);
  console.log(list);

  var rfcList =  $().SPServices.SPGetListItemsJson({
                  webURL: url,
                  async: false,  //if you set this to true, you may get faster performance, but your order may not be accurate.
                  listName: list,
                  CAMLViewFields: fieldsToRead,
                  CAMLQuery: query,
                  debug: true
               });
  for (var key in rfcList) {
      console.log("key " + key + " has value " + rfcList[key]);
   }
};

The page source of list I am reading with 2 records is as follows:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="<**BASE_URL**>/_vti_bin/listdata.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">RFCExtract</title>
  <id><**BASE_URL**>/_vti_bin/listdata.svc/RFCExtract</id>
  <updated>2018-11-29T12:43:44Z</updated>
  <link rel="self" title="RFCExtract" href="RFCExtract" />
  <entry m:etag="W/&quot;1&quot;">
    <id><**BASE_URL**>/_vti_bin/listdata.svc/RFCExtract(1)</id>
    <title type="text">Test</title>
    <updated>2018-11-28T15:07:57-05:00</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="RFCExtractItem" href="RFCExtract(1)" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="RFCExtract(1)/CreatedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="RFCExtract(1)/ModifiedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="RFCExtract(1)/Attachments" />
    <category term="Microsoft.SharePoint.DataService.RFCExtractItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:ContentTypeID>0x010018E49BFAAA76C74C8D9E319D13220A60</d:ContentTypeID>
        <d:Title>Test</d:Title>
        <d:Class m:type="Edm.Double">1</d:Class>
        <d:Status>Closed</d:Status>
        <d:Date m:type="Edm.DateTime">2010-11-01T00:00:00</d:Date>
        <d:RFCNumber m:type="Edm.Double">1</d:RFCNumber>
        <d:Id m:type="Edm.Int32">1</d:Id>
        <d:ContentType>Item</d:ContentType>
        <d:Modified m:type="Edm.DateTime">2018-11-28T15:07:57</d:Modified>
        <d:Created m:type="Edm.DateTime">2018-11-28T15:07:57</d:Created>
        <d:CreatedById m:type="Edm.Int32">9929</d:CreatedById>
        <d:ModifiedById m:type="Edm.Int32">9929</d:ModifiedById>
        <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion>
        <d:Version>1.0</d:Version>
        <d:Path>/sp/CCSB/Lists/RFC Extract</d:Path>
      </m:properties>
    </content>
  </entry>
  <entry m:etag="W/&quot;1&quot;">
    <id><**BASE_URL**>/_vti_bin/listdata.svc/RFCExtract(2)</id>
    <title type="text">Test Record 2</title>
    <updated>2018-11-29T07:42:19-05:00</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="RFCExtractItem" href="RFCExtract(2)" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="RFCExtract(2)/CreatedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="RFCExtract(2)/ModifiedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="RFCExtract(2)/Attachments" />
    <category term="Microsoft.SharePoint.DataService.RFCExtractItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:ContentTypeID>0x010018E49BFAAA76C74C8D9E319D13220A60</d:ContentTypeID>
        <d:Title>Test Record 2</d:Title>
        <d:Class m:type="Edm.Double">2</d:Class>
        <d:Status>Pending</d:Status>
        <d:Date m:type="Edm.DateTime">2018-11-02T00:00:00</d:Date>
        <d:RFCNumber m:type="Edm.Double">2</d:RFCNumber>
        <d:Id m:type="Edm.Int32">2</d:Id>
        <d:ContentType>Item</d:ContentType>
        <d:Modified m:type="Edm.DateTime">2018-11-29T07:42:19</d:Modified>
        <d:Created m:type="Edm.DateTime">2018-11-29T07:42:19</d:Created>
        <d:CreatedById m:type="Edm.Int32">9929</d:CreatedById>
        <d:ModifiedById m:type="Edm.Int32">9929</d:ModifiedById>
        <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion>
        <d:Version>1.0</d:Version>
        <d:Path>/sp/CCSB/Lists/RFC Extract</d:Path>
      </m:properties>
    </content>
  </entry>
</feed>
3

In SharePoint 2010, if you want to get list items, we can easily use REST API with jQuery Ajax to achieve it. Don't need use the SPService library. The following code for your reference.

<script src="https://code.jquery.com/jquery-1.12.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    $("#getListUsingSPSerivces").click(function(){
        getListUsingSPSerivces();
    });
});
function getListUsingSPSerivces(){
    var siteUrl="http://sp2010";
    var listName="RFCExtract";
    var readFields="RFCNumber,Title,Status,Date,Class";
    var filter="RFCNumber gt 0";
    var orderby="RFCNumber";

    var requestUri =siteUrl+"/_vti_bin/listdata.svc/"+listName+"?$select="+readFields+"&$filter="+filter+"&$orderby="+orderby;
    $.ajax({
        url: requestUri,
        method: "GET",
        async:false,
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            var items=data.d.results;
            $.each(items,function(i,item){
                var dateField="";
                if(item.Date!=null){
                    var date=parseJsonDate(item.Date);
                    dateField=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
                }
                console.log("RFCNumber:"+item.RFCNumber+" Title:"+item.Title+" Status:"+item.Status+" Date:"+dateField+" Class:"+item.Class);
            });
        },
        error: function (err) {
            console.log(JSON.stringify(err));
        }
    });
};
function parseJsonDate( sDate ) {
    var b, e, i;
    b = sDate.indexOf('(');
    e = sDate.indexOf(')');
    i = sDate.substring(b+1,e);

    if (isNaN(i)) { return null };
    return new Date(parseInt(i));
}
</script>
<input id="getListUsingSPSerivces" type="button" value="getListUsingSPSerivces"/>

enter image description here

  • Thank you. Could I reference you to my other question which is exactly this same process. I am having many problems right now dealing with SP. Please refer to this question for I am struggling to read a SP list. I will be comparing your response to mine shortly. Error reading syndication item: 'Data at the root level is invalid. Line 1, position 1.' – mcv Nov 30 '18 at 15:04
  • I have implemented your method as described above. I received a new error which I can easily fix, but now this error will not change, even though I removed the Query parameters it complained about so that the requestUri = "<BASE_URL>/_vti_bin/listdata.svc/RFCExtract?$select=RFCNumber,Title,Status,Date,Class" and now the error I am constantly seeing is: "{ "error": { "code": "", "message": { "lang": "en-CA", "value": "Query options $expand, $filter, $orderby, $inlinecount, $skip and $top cannot be applied to the requested resource." } } }" – mcv Nov 30 '18 at 15:50
  • @LZ_MST YAY Thank you!!!! I noted that I omitted the method, I used POST not GET. Why is that the case? This method was my first attempt and it was the most promising before I looked into SPServices. Now I have the list being read. I'm going to reference this in my other post and accept it as the answer. – mcv Nov 30 '18 at 16:05

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.