1

This is the first time I have encountered a REST API get returning some items in a list inside a collection but not others. Here's an example:

    jQuery.ajax({
    url: fullUrl,
    type: "GET",
    async: false,
    headers: {
        "accept": "application/json;odata=verbose",
        "content-type": "application/json,odata=verbose",
    },
    success: function(productData){
             ... do something ...
    }
});


REST URL: [site url]/_api/web/lists/GetByTitle('Products')/items(6)

Returns

{
"odata.metadata": "[site url]/_api/$metadata#SP.ListData.ProductsListItems/@Element",
"odata.type": "SP.Data.ProductsListItem",
"odata.id": "7ccff0bc-c97d-4ba3-86e7-a487c251ca6e",
"odata.etag": "\"2\"",
"odata.editLink": "Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(6)",
"FileSystemObjectType": 0,
"Id": 6,
"ServerRedirectedEmbedUri": null,
"ServerRedirectedEmbedUrl": "",
"ContentTypeId": "0x0100C25F3CFD70B42D4DBE5D3FC6E2845A9F",
"Title": "My title",
"Description": "Installation, design, and development of something",
"ProcessId": null,
"Price": null,
"Item_x0020_Type": "Service",
"Category": null,
"VendorItem": null,
"ID": 6,
"Modified": "2017-03-09T19:22:08Z",
"Created": "2016-12-20T20:20:53Z",
"AuthorId": 10,
"EditorId": 10,
"OData__UIVersionString": "1.0",
"Attachments": false,
"GUID": "32ce2eb7-8262-43f2-87a2-6e266d787894"

}

However:

REST URL: [site url]/_api/web/lists/GetByTitle('Products')/items(3)

Returns in a collection "d"

{
"d": {
    "__metadata": {
        "id": "0b91b075-671f-4774-9e91-948f078fafad",
        "uri": "[site url]/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)",
        "etag": "\"5\"",
        "type": "SP.Data.ProductsListItem"
    },
    "FirstUniqueAncestorSecurableObject": {
        "__deferred": {
            "uri": "[site url]/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/FirstUniqueAncestorSecurableObject"
        }
    },
    "RoleAssignments": {
        "__deferred": {
            "uri": "[site url]/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/RoleAssignments"
        }
    },
    "Activities": {
        "__deferred": {
            "uri": "[site url]/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/Activities"
        }
    },
    "AttachmentFiles": {
        "__deferred": {
            "uri": "https://strattechbiz.sharepoint.com/CPMDevV1/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/AttachmentFiles"
        }
    },
    "ContentType": {
        "__deferred": {
            "uri": "https://strattechbiz.sharepoint.com/CPMDevV1/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/ContentType"
        }
    },
    "GetDlpPolicyTip": {
        "__deferred": {
            "uri": "https://strattechbiz.sharepoint.com/CPMDevV1/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/GetDlpPolicyTip"
        }
    },
    "FieldValuesAsHtml": {
        "__deferred": {
            "uri": "https://strattechbiz.sharepoint.com/CPMDevV1/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/FieldValuesAsHtml"
        }
    },
    "FieldValuesAsText": {
        "__deferred": {
            "uri": "https://strattechbiz.sharepoint.com/CPMDevV1/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/FieldValuesAsText"
        }
    },
    "FieldValuesForEdit": {
        "__deferred": {
            "uri": "https://strattechbiz.sharepoint.com/CPMDevV1/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/FieldValuesForEdit"
        }
    },
    "File": {
        "__deferred": {
            "uri": "https://strattechbiz.sharepoint.com/CPMDevV1/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/File"
        }
    },
    "Folder": {
        "__deferred": {
            "uri": "https://strattechbiz.sharepoint.com/CPMDevV1/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/Folder"
        }
    },
    "ParentList": {
        "__deferred": {
            "uri": "https://strattechbiz.sharepoint.com/CPMDevV1/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/ParentList"
        }
    },
    "Properties": {
        "__deferred": {
            "uri": "https://strattechbiz.sharepoint.com/CPMDevV1/_api/Web/Lists(guid'4e866caa-513a-44ea-ba85-c84fff621d5d')/Items(3)/Properties"
        }
    },
    "FileSystemObjectType": 0,
    "Id": 3,
    "ServerRedirectedEmbedUri": null,
    "ServerRedirectedEmbedUrl": "",
    "ContentTypeId": "0x0100C25F3CFD70B42D4DBE5D3FC6E2845A9F",
    "Title": "On-line Store Development",
    "Description": "Installation, design, and development of on-line store",
    "ProcessId": 3,
    "Price": 100.5,
    "Item_x0020_Type": "Service",
    "Category": null,
    "VendorItem": null,
    "ID": 3,
    "Modified": "2017-05-22T17:23:00Z",
    "Created": "2016-12-20T20:20:53Z",
    "AuthorId": 10,
    "EditorId": 10,
    "OData__UIVersionString": "1.0",
    "Attachments": false,
    "GUID": "65c71feb-e09f-4174-a1c8-3ac69861b7c4"
}

}

Why is this happening? And what is the best way to handle this in javascript/jQuery?

  • Are these returning same output when you run these from the browser? – Venkat Konjeti May 22 '17 at 19:13
  • I get the same results as shown above regardless of using a REST utility or my javascript program. – maleman May 22 '17 at 19:29
  • you have a comma instead of a semi-colon in your content-type header. – Aveenav May 22 '17 at 20:45
  • also try eliminating content-type altogether, just accept would suffice. – Aveenav May 22 '17 at 20:54
0

Well it appears that I may had a discrepancy between my REST Console utility and what my code was actually seeing. All of the items being seen by the code is in a collection "d" with just a single item in it. I discovered the discrepancies after adding the following code and noticing that the "else" was never executed when I stepped through the program!

                if (data.d) {
                    var productData = data.d;
                } else {
                    productData = data;
                }

I went back to my REST Console utility and noticed that the URL had double / in it! [site url]//_api/web/lists.... What is interesting is that it actually returned a valid object with the correct data in it, just not wrapped in a collection.

Thanks to all who helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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