Pagination - SDK examples
Here are Python, Java, JavaScript/Node.js, and C# code examples of paging through items (in this case, workspace references) using Smartsheet SDKs. The examples use token-based pagination.
Python SDK
Here's Python code that uses the Smartsheet Python SDK to page through Smartsheet workspaces.
import smartsheet
smart = smartsheet.Smartsheet(
access_token='YOUR_API_TOKEN', # Replace with your token value
)
page = smart.Workspaces.list_workspaces(
pagination_type='token',
max_items=100
)
assert isinstance(page, smartsheet.models.IndexResult)
print(page.to_dict()) # Do what you want with the page items
last_key = get_attr(page, last_key, "")
while last_key != "":
page = smart.Workspaces.list_workspaces(
pagination_type='token',
max_items=100,
last_key=page.last_key
)
assert isinstance(page, smartsheet.models.IndexResult)
print(page.to_dict()) # Do what you want with the page items
last_key = get_attr(page, last_key, "")The first smart.Workspaces.list_workspaces(...) call returns up to 100 workspace references.
The assert isinstance(page, smartsheet.models.IndexResult) call checks that the response matches the expected schema (that is, smartsheet.models.IndexResult). If it doesn't match, the function raises an AsserError. Otherwise, processing continues.
Note: The
# Do what you want ...comments mark optimal places for processing the workspace references.
If the page includes a last_key attribute, there are more workspace references that you can retrieve.
The while last_key != "": loop body exercises the same logic on the next page of items. In it, you can act on the items.
Looping stops when the smart.Workspaces.list_workspaces(...) response no longer includes a last_key attribute.
JavaScript SDK (can use in Node.js)
Here's JavaScript code that uses the Smartsheet JavaScript SDK to page through Smartsheet workspaces.
Note: You can use the JavaScript SDK in Node.js apps.
import smartsheet from "smartsheet";
const smart = smartsheet.createClient({
accessToken: "YOUR_API_TOKEN", // Replace with your token value
});
const page = await smart.workspaces.listWorkspaces({
queryParameters: { paginationType: "token", maxItems: 100 },
});
console.log(page); // Do what you want with the page items
let lastKey = page.lastKey;
while (lastKey) {
const page = await smart.workspaces.listWorkspaces({
queryParameters: {
paginationType: "token",
maxItems: 100,
lastKey: lastKey,
},
});
console.log(page); // Do what you want with the page items
lastKey = page.lastKey;
}The first smart.workspaces.listWorkspaces(...) call returns up to 100 workspace references.
Note: The
# Do what you want ...comments mark optimal places for processing the workspace references.
If the page includes a lastKey attribute, there are more workspace references that you can retrieve.
The while (lastKey) loop body exercises the same logic on the next page of items. In it, you can act on the items.
Looping stops when the smart.workspaces.listWorkspaces(...) response no longer includes a lastKey attribute.
Java SDK
Here's Java code that uses the Smartsheet Java SDK to page through Smartsheet workspaces.
package com.example.job;
import com.smartsheet.api.Smartsheet;
import com.smartsheet.api.SmartsheetException;
import com.smartsheet.api.SmartsheetFactory;
import com.smartsheet.api.models.PagedResult;
import com.smartsheet.api.models.PaginationParameters;
public class Main {
public static void main(String[] args) {
try {
String accessToken = "YOUR_API_TOKEN"; // Replace with your token value
Smartsheet smart = SmartsheetFactory.createDefaultClient(accessToken);
PagedResult page = smart.workspaceResources().listWorkspaces(new PaginationParameters("token", null, 100));
System.out.println(page); // Do what you want with the page items
String lastKey = page.getLastKey();
while (lastKey != null) {
page = smart.workspaceResources().listWorkspaces(new PaginationParameters("token", lastKey, 100));
System.out.println(page); // Do what you want with the page items
lastKey = page.getLastKey();
}
} catch (SmartsheetException e) {
System.err.println("SmartsheetException occurred: " + e.getMessage());
}
}
}First it calls smart.workspaceResources().listWorkspaces(...) with the API token, null as the last key, and 100 as the maximum number of items on a page. The call returns up to 100 workspace references.
Note: The
# Do what you want ...comments mark optimal places for processing the workspace references.
If the page includes a non-null lastKey attribute, there are more workspace references that you can retrieve.
The while (lastKey != null) loop body exercises the same logic on the next page of items. In it, you can act on the items.
Looping stops when the smart.workspaceResources().listWorkspaces(...) response's lastKey attribute is null.
C# SDK
Here's C# code that uses the Smartsheet C# SDK to page through Smartsheet workspaces.
using Smartsheet.Api;
using Smartsheet.Api.Models;
namespace SmartsheetDemo
{
class Program
{
static void Main(string[] args)
{
try
{
string accessToken = "YOUR_API_TOKEN";
SmartsheetClient smart = new SmartsheetBuilder()
.SetAccessToken(accessToken)
.Build();
TokenPaginatedResult<Workspace> page = smart.WorkspaceResources.ListWorkspaces(
new ListWorkspacesTokenPaginationParameters(null, 100)
);
Console.WriteLine(page); // Do what you want with the page items
string? lastKey = page.LastKey;
while (lastKey != null)
{
page = smart.WorkspaceResources.ListWorkspaces(
new ListWorkspacesTokenPaginationParameters(lastKey, 100)
);
Console.WriteLine(page); // Do what you want with the page items
lastKey = page.LastKey;
}
}
catch (Exception ex)
{
Console.WriteLine($"SmartsheetException occurred: {ex.Message}");
}
}
}
}First it calls smart.WorkspaceResources.ListWorkspaces(...) with the API token, null for the lastKey, and 100 as the maximum number of items on a page. The call returns up to 100 workspace references.
Note: The
# Do what you want ...comments mark optimal places for processing the workspace references.
If the page includes a non-null lastKey attribute, there are more workspace references that you can retrieve.
The while (lastKey != null) loop body exercises the same logic on the next page of items. In it, you can act on the items.
Looping stops when the smart.WorkspaceResources.ListWorkspaces(...) response's lastKey attribute is null.
You've now examined C#, Java, JavaScript, and Python code examples that demonstrate paging through workspace references. You can use the same approach on other operations that support token-based pagination.