INNVTV
INNVTV

Reputation: 3367

Microsoft.Azure.Cosmos SDK Paginated Queries with Continuation Token

I'm migrating from the Microsoft.Azure.Documents SDK to the new Microsoft.Azure.Cosmos (v3.2.0) and am having an issue getting a continuation token back for paginated queries. In the previous SDK when you had the FeedResponse object it returned a bool for HasMoreResults as well as a ContinuationToken which I pass off to my users should they want to make a call for the next page (via an API endpoint). In the new SDK I am trying to use the GetItemQueryIterator method on my container and the only examples I see are using a while loop to get all the pages using the HasMoreResults value with no way for me to extract a ContinuationToken and just pass back the first set of results.

Here is how my code looks so far:

var feedIterator = _documentContext.Container.GetItemQueryIterator<MyDocumentModel>(query, request.ContinuationToken, options);

if (feedIterator.HasMoreResults)
{
    listViewModel.HasMoreResults = true;
    //listViewModel.ContinuationToken = feedIterator.ContinuationToken; (DOES NOT EXIST!)
}

The commented out line is where I would usually expect to extract the ContinuationToken from but it does not exist.

Most examples show using the iterator like so:

while (feedIterator.HasMoreResults)
{
    listViewModel.MyModels.AddRange(_mapper.Map<List<MyModelListViewItem>>(await results.ReadNextAsync()));
}

But I only want to return a single page of results and pass in a continuation token if I want to get the next page.

Upvotes: 0

Views: 2221

Answers (2)

Matias Quaranta
Matias Quaranta

Reputation: 15583

The ContinuationToken is part of the ReadNextAsync response:

FeedResponse<MyDocumentModel> response = await feedIterator.ReadNextAsync();
var continuation = response.ContinuationToken;

The reason is that the ReadNextAsync is the moment where the service call is made, representing a page of data, and the continuation is for that particular page.

Upvotes: 4

Gaurav Mantri
Gaurav Mantri

Reputation: 136126

I have not tried the code but looking through the documentation, ReadNextAsync() method on FeedIterator returns you an object of type FeedResponse and that has a ContinuationToken property.

Upvotes: 2

Related Questions