Sort and Filter

Several of the Files.com API resources have list operations that return multiple instances of the resource. The List operations can be sorted and filtered.

Sorting

To sort the returned data, pass in the sort_by method argument.

Each resource supports a unique set of valid sort fields and can only be sorted by one field at a time.

The argument value is a Javascript object that has a property of the resource field name sort on and a value of either "asc" or "desc" to specify the sort order.

Special note about the List Folder Endpoint

For historical reasons, and to maintain compatibility with a variety of other cloud-based MFT and EFSS services, Folders will always be listed before Files when listing a Folder. This applies regardless of the sorting parameters you provide. These will be used, after the initial sort application of Folders before Files.

Sort Example

import User from 'files.com/lib/models/User';
import * as FilesErrors from 'files.com/lib/Errors';

try {
  // Users, sorted by username in ascending order.
  const users = await User.list({
    sort_by: { username: "asc" }
  });
  
  users.forEach(user => {
    console.log(user.username);
  });
} catch (err) {
  if (err instanceof FilesErrors.NotAuthenticatedError) {
    console.error(`Authentication Error Occurred (${err.constructor.name}): ${err.error}`);
  } else if (err instanceof FilesErrors.FilesError) {
    console.error(`Unknown Error Occurred (${err.constructor.name}): ${err.error}`);
  } else {
    throw err;
  }
}

Filtering

Filters apply selection criteria to the underlying query that returns the results. They can be applied individually or combined with other filters, and the resulting data can be sorted by a single field.

Each resource supports a unique set of valid filter fields, filter combinations, and combinations of filters and sort fields.

The passed in argument value is a Javascript object that has a property of the resource field name to filter on and a passed in value to use in the filter comparison.

Filter Types

FilterTypeDescription
filterExactFind resources that have an exact field value match to a passed in value. (i.e., FIELD_VALUE = PASS_IN_VALUE).
filter_prefixPatternFind resources where the specified field is prefixed by the supplied value. This is applicable to values that are strings.
filter_gtRangeFind resources that have a field value that is greater than the passed in value. (i.e., FIELD_VALUE > PASS_IN_VALUE).
filter_gteqRangeFind resources that have a field value that is greater than or equal to the passed in value. (i.e., FIELD_VALUE >= PASS_IN_VALUE).
filter_ltRangeFind resources that have a field value that is less than the passed in value. (i.e., FIELD_VALUE < PASS_IN_VALUE).
filter_lteqRangeFind resources that have a field value that is less than or equal to the passed in value. (i.e., FIELD_VALUE <= PASS_IN_VALUE).

Exact Filter Example

import User from 'files.com/lib/models/User';
import * as FilesErrors from 'files.com/lib/Errors';

try {
  // Users who are not site admins.
  const users = await User.list({
    filter: { not_site_admin: true }
  });
  
  users.forEach(user => {
    console.log(user.username);
  });
} catch (err) {
  if (err instanceof FilesErrors.NotAuthenticatedError) {
    console.error(`Authentication Error Occurred (${err.constructor.name}): ${err.error}`);
  } else if (err instanceof FilesErrors.FilesError) {
    console.error(`Unknown Error Occurred (${err.constructor.name}): ${err.error}`);
  } else {
    throw err;
  }
}

Range Filter Example

import User from 'files.com/lib/models/User';
import * as FilesErrors from 'files.com/lib/Errors';

try {
  // Users who haven't logged in since 2024-01-01.
  const users = await User.list({
    filter_gteq: { last_login_at: "2024-01-01" }
  });
  
  users.forEach(user => {
    console.log(user.username);
  });
} catch (err) {
  if (err instanceof FilesErrors.NotAuthenticatedError) {
    console.error(`Authentication Error Occurred (${err.constructor.name}): ${err.error}`);
  } else if (err instanceof FilesErrors.FilesError) {
    console.error(`Unknown Error Occurred (${err.constructor.name}): ${err.error}`);
  } else {
    throw err;
  }
}

Pattern Filter Example

import User from 'files.com/lib/models/User';
import * as FilesErrors from 'files.com/lib/Errors';

try {
  // Users whose usernames start with 'test'.
  const users = await User.list({
    filter_prefix: { username: "test" }
  });
  
  users.forEach(user => {
    console.log(user.username);
  });
} catch (err) {
  if (err instanceof FilesErrors.NotAuthenticatedError) {
    console.error(`Authentication Error Occurred (${err.constructor.name}): ${err.error}`);
  } else if (err instanceof FilesErrors.FilesError) {
    console.error(`Unknown Error Occurred (${err.constructor.name}): ${err.error}`);
  } else {
    throw err;
  }
}

Combination Filter with Sort Example

import User from 'files.com/lib/models/User';
import * as FilesErrors from 'files.com/lib/Errors';

try {
  // Users whose usernames start with 'test' and are not site admins, sorted by last login date.
  const users = await User.list({
    filter_prefix: { username: "test" },
    filter: { not_site_admin: true },
    sort_by: { last_login_at: "asc" }
  });
  
  users.forEach(user => {
    console.log(user.username);
  });
} catch (err) {
  if (err instanceof FilesErrors.NotAuthenticatedError) {
    console.error(`Authentication Error Occurred (${err.constructor.name}): ${err.error}`);
  } else if (err instanceof FilesErrors.FilesError) {
    console.error(`Unknown Error Occurred (${err.constructor.name}): ${err.error}`);
  } else {
    throw err;
  }
}