NextToken is not empty when reached to last page in search_dashboards API

Hi,

I am using quicksight API search_dashboards to return dashboards list of my account. However, I found that the NextToken is not null when the last page fetched from the page. Suppose I have 75 dashboards in my quicksight, ideally the 3rd time API call will return the NextToken to null. However, it didn’t. Very appreciate your support.

import boto3

# Replace 'your_aws_region' with your AWS region
aws_region = 'us-east-1'

# Create a Boto3 QuickSight client
quicksight_client = boto3.client('quicksight', region_name=aws_region)


def list_quicksight_dashboards():
    try:
        # List dashboards
        response = quicksight_client.search_dashboards(
            AwsAccountId='123',
            Filters=[
                {
                    'Operator': 'StringEquals',
                    'Name': 'QUICKSIGHT_USER',
                    'Value': 'abc123'
                },
            ],
            MaxResults=25,
        )

        # Print information about each dashboard
        print("total dashboards: ", len(response["DashboardSummaryList"]))
        while response.get("NextToken"):
            response = quicksight_client.search_dashboards(
                AwsAccountId='123',
                Filters=[
                    {
                        'Operator': 'StringEquals',
                        'Name': 'QUICKSIGHT_USER',
                        'Value': 'abc123'
                    },
                ],
                MaxResults=25,
                NextToken=response["NextToken"]
            )
            print("total dashboards: ", len(response["DashboardSummaryList"]))


    except Exception as e:
        print(f"Error: {e}")


if __name__ == "__main__":
    list_quicksight_dashboards()

>>>> output:
total dashboards:  25
total dashboards:  25
total dashboards:  25
total dashboards:  0

@ryang Interesting, I haven’t used the search, but used their list API,
I do

if response['Status'] == 200:
    next_roken = response['NextToken']

which has worked for me. Can you try that? if not I think this might be an issue, and you can report it.

1 Like

@ryang

I tried it myself experimenting with different values for MaxResults. In my example, I have 14 dashboards. If I use any value for MaxResults other than 1, 2, 7 or 14, everything works as expected.
In other words, when the remaining number of dashboards is divisible by the maxResults value, then it runs again and yields total dashboards = 0



I think it is the algorithm implemented in NextToken.
In the meantime, if this is a blocker, you can use list-dashboards as @neelay suggested.

Did this response work for you? I am marking this reply as, “Solution,” but let us know if this is not resolved. Thanks for posting your questions on the QuickSight Community Q&A Forum!

GL
:slight_smile:

1 Like

list-dashboards works and no issue. But it returns all dashboards under an aws account, instead of a specific quicksight user.

@ryang

You can find the users in list-dashboard-permissions. This is how you can get the dashboards’ names for a specific user:

import boto3
qs = boto3.client('quicksight')

user = 'the username you are looking for (or part of it)'

for d in qs.list_dashboards(AwsAccountId='XXXXXXXXXXXX')['DashboardSummaryList']:
    perm = qs.describe_dashboard_permissions(AwsAccountId='XXXXXXXXXXXX', DashboardId=d['DashboardId'])['Permissions']
    for p in perm:
        if user in p['Principal']:
            print("User Name: ", p['Principal'])
            print("Dashboard Name: ", d['Name'])
            print("\n")

Hope it helps,
GL