6

I have a Blog subsite of my main website. I am trying to get the Top 2 blog posts along with the number of comments and actual comments. I am able to get Number of Comments with the field NumCommentsId but along with it i also want Comments to be part of the JSON object returned from the REST API. This is how my url looks like:

http://mywebsite-dev/blog/_api/web/lists/getByTitle('Posts')/items?$select=Title,Body,Id,LikesCount,NumCommentsId

This is how the returned JSON looks like:

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "id": "Web/Lists(guid'862870c0-e56b-48ca-9da4-b337d05984aa')/Items(2)",
                    "uri": "http://mywebsite-dev/blog/_api/Web/Lists(guid'862870c0-e56b-48ca-9da4-b337d05984aa')/Items(2)",
                    "etag": "\"1\"",
                    "type": "SP.Data.PostsListItem"
                },
                "Id": 2,
                "Title": "Blog 1",
                "Body": "<div class=\"ExternalClass30B2DE63AD2D4EE1AC4DD2735266245C\"><p>This is my blog 1•</p></div>",
                "NumCommentsId": 0,
                "LikesCount": null,
                "ID": 2
            },
            {
                "__metadata": {
                    "id": "Web/Lists(guid'862870c0-e56b-48ca-9da4-b337d05984aa')/Items(3)",
                    "uri": "http://mywebsite-dev/blog/_api/Web/Lists(guid'862870c0-e56b-48ca-9da4-b337d05984aa')/Items(3)",
                    "etag": "\"1\"",
                    "type": "SP.Data.PostsListItem"
                },
                "Id": 3,
                "Title": "Blog 2",
                "Body": "<div class=\"ExternalClass9FDE8E9403FF43F3BEE2BC24798EDA16\"><p>This is second cloud blog•</p></div>",
                "NumCommentsId": 3,
                "LikesCount": null,
                "ID": 3
            }
        ]
    }
}

I want actual comments to be returned in this JSON object. Something like this:

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "id": "Web/Lists(guid'862870c0-e56b-48ca-9da4-b337d05984aa')/Items(2)",
                    "uri": "http://mywebsite-dev/blog/_api/Web/Lists(guid'862870c0-e56b-48ca-9da4-b337d05984aa')/Items(2)",
                    "etag": "\"1\"",
                    "type": "SP.Data.PostsListItem"
                },
                "Id": 2,
                "Title": "Blog 1",
                "Body": "<div class=\"ExternalClass30B2DE63AD2D4EE1AC4DD2735266245C\"><p>This is my blog 1•</p></div>",
                "NumCommentsId": 0,
        "Comments":{}
                "LikesCount": null,
                "ID": 2
            },
            {
                "__metadata": {
                    "id": "Web/Lists(guid'862870c0-e56b-48ca-9da4-b337d05984aa')/Items(3)",
                    "uri": "http://mywebsite-dev/blog/_api/Web/Lists(guid'862870c0-e56b-48ca-9da4-b337d05984aa')/Items(3)",
                    "etag": "\"1\"",
                    "type": "SP.Data.PostsListItem"
                },
                "Id": 3,
                "Title": "Blog 2",
                "Body": "<div class=\"ExternalClass9FDE8E9403FF43F3BEE2BC24798EDA16\"><p>This is second cloud blog•</p></div>",
                "NumCommentsId": 3,
        "Comments":[
                {
                "Id":1,
                "Title":"My Comment Title",
                "Body":"My Comment Body"
                },
                {
                "Id":2,
                "Title":"My second Comment Title",
                "Body":"My second Comment Body"
                },

                {
                "Id":3,
                "Title":"My third Comment Title",
                "Body":"My third Comment Body"
                },
               ]
                "LikesCount": null,
                "ID": 3
            }
        ]
    }
}

Is this possible? If so, how?

  • I took a look and, based on the way the lookup fields are implemented, I'm pretty sure you can't get the posts and the comments with a single REST API call. – Rob Windsor Oct 31 '14 at 17:59
1

it's not possible with REST API.

Query for multi-value lookup fields Because multi-value lookup fields are returned as a string of multiple values, there is no way to query for them (for example, the equivalent of an Includes element or NotIncludes element is not supported).

http://msdn.microsoft.com/en-us/library/office/fp142385(v=office.15).aspx#sectionSection4

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.