Cant get pagination to work in python

I can’t get pagination to work
The following call return 100 lines exactly (although there are many more)
the next_page is not to be found anywhere
HELP !!

result = client.tasks.search_tasks_for_workspace(WORKSPACE_GID,
opt_fields=[“name”, “gid”,
“assignee.name”,
“created_by.name”,
“created_at”,“projects.name”])
for r in result:
print(r)

If I am not mistaken, the search endpoint is not paginated automatically like others with the Asana libs, you have to do the pagination yourself, usually using the creation date of tasks. That’s what I do in my case.

I sort by created_date, then request the first page, get the creation date of the last task, and ask for the next page.

@Phil_Seeman can you confirm?

2 Likes

@Bastien_Siebman is exactly correct, no pagination in the Search API. See the paragraph titled “Pagination” here in the docs:
https://developers.asana.com/docs/search-tasks-in-a-workspace

Thanks for the quick response …
Is there a code example to show how it’s done

Hold on I’ll share part of my javascript code

this.query = {
      limit: 100,
      opt_fields: 'parent.gid,parent.name,name,created_at,parent.completed,is_rendered_as_separator',
      completed: false,
      sort_by: 'created_at' // default is descending order
    }

then calling the search code which basically looks like this

 search(createdAtBefore?: string) {
    if (createdAtBefore) {
      this.query['created_at.before'] = createdAtBefore
    }

      this.asanaTaskService.search(this.organizationId, this.query).subscribe((collection: any) => {
        collection.stream().on('data', task => {
            this.lastDate = task.created_at
            ...
            }
          })
          .on('end', () => {
              // on page end, search next page
                this.search(this.lastDate)
            })
          })
      })
...

@Sharon_Cohen-Ofir that should get you going. Right here is the result of a handful of struggling coding sessions :grimacing:

Thanks
Below you can find my WORKING python code, in case somebody needs it down the road

found = True
while found:
    result = client.tasks.search_tasks_for_workspace(WORKSPACE_GID,
                                                     {"created_at.before" : last_date },
                                                     sort_by="created_at",
                                                     opt_fields=["name", "gid",
                                                                 "assignee.name",
                                                                 "created_by.name",
                                                                 "created_at","projects.name"])
    found = False
    for r in result:
        found = True
        last_date = r["created_at"]
        <do your processing>
3 Likes

Seems I also had the same question. Thanks for yours solutions.