Get Views

Maintained on

kintone offers a rich set of APIs that allow you to directly manipulate application data from your programs.

In particular, the REST API is a powerful tool for manipulating kintone data through HTTP requests.

This page provides a detailed explanation of how to retrieve views of a specific app using the kintone REST API.

Using from kintone

When executing the REST API as a kintone JavaScript customization, you can easily execute the REST API by using the provided JavaScript API.

By using the kintone.api.url method of the JavaScript API, you can obtain the REST API endpoint without being aware of the execution space.

Definition_of_a_generic_function
/**
 * Retrieve app views using the kintone REST API
 *
 * @param { { app: string | number; lang?: "default" | "user" | "ja" | "en" | "zh" }  } params
 * @return { Promise<{ views: ({ builtinType?: 'ASSIGNEE'; name: string; id: string; filterCond: string; sort: string; index: string; } & ({ type: 'LIST'; fields: string[]; } | { type: 'CALENDAR'; date: string; title: string; } | { type: 'CUSTOM'; html: string; pager: boolean; device: 'DESKTOP' | 'ANY'; }))[]; revision: string; }> } - Retrieved views and revision number
 */
const getViews = (params) => {
  return kintone.api(kintone.api.url('/k/v1/app/views.json', true), 'GET', params);
};

Specify the app ID to reference and the language settings as arguments. Language settings are optional.

There are list views, calendar views, and custom views. Each view type includes different properties.

Sample Code

Sample_code_to_retrieve_views_of_the_currently_displayed_app
(() => {
  const getViews = (params) => {
    /** Omitted */
  };

  kintone.events.on(['app.record.index.show'], async (event) => {
    const app = kintone.app.getId();
    const { views } = await getViews({ app });

    for (const view of views) {
      console.log(view);
    }

    return event;
  });
})();
Sample_code_to_retrieve_views_of_an_app_specified_by_the_user
(() => {
  const getViews = (params) => {
    /** Omitted */
  };

  kintone.events.on(['app.record.index.show'], async (event) => {
    const app = prompt('Please enter the ID of the app to retrieve');
    const { views } = await getViews({ app });

    for (const view of views) {
      console.log(view);
    }

    return event;
  });
})();

Using from outside kintone

The kintone REST API can be used from various environments such as Excel, GAS, and Node.js.

By using the REST API, you can operate applications using kintone data from environments other than kintone.

Use API Tokens

When retrieving kintone data from environments other than kintone, you need to include authentication information in the request.

There are several types of authentication information, but using an API token allows you to easily obtain authentication information while ensuring security.

The simplest method is to use an ID and password, but due to security risks, it is recommended to use an API token with the minimum necessary permissions.

Using Node.js

When using the kintone REST API from Node.js, you can send HTTP requests with almost the same code as executing JavaScript in the browser.

/**
 * Retrieve app views using the kintone REST API
 *
 * @param { { app: string | number; lang?: "default" | "user" | "ja" | "en" | "zh" }  } params
 * @return { Promise<{ views: ({ builtinType?: 'ASSIGNEE'; name: string; id: string; filterCond: string; sort: string; index: string; } & ({ type: 'LIST'; fields: string[]; } | { type: 'CALENDAR'; date: string; title: string; } | { type: 'CUSTOM'; html: string; pager: boolean; device: 'DESKTOP' | 'ANY'; }))[]; revision: string; }> } - Retrieved views and revision number
 */
const getViews = (params) => {
  const url = `https://__your_subdomain__.cybozu.com/k/v1/app/views.json?${new URLSearchParams(params).toString()}`;
  const options = {
    method: 'GET',
    headers: {
      'X-Cybozu-API-Token': 'YOUR_API_TOKEN',
    },
  };
  const response = await fetch(url, options);
  return response.json();
};

Replace __your_subdomain__ and YOUR_API_TOKEN with your own environment settings.

You can set these as arguments, but it is also fine to set them directly within the function.

Unlike the code used from kintone, if the referenced app is in a guest space, you need to change the URL.

Using GAS

When using the kintone REST API from Google Apps Script (GAS), you can send HTTP requests using the UrlFetchApp class.

/**
 * Retrieve app views using the kintone REST API
 *
 * @param { { app: string | number; lang?: "default" | "user" | "ja" | "en" | "zh" }  } params
 * @return { Promise<{ views: ({ builtinType?: 'ASSIGNEE'; name: string; id: string; filterCond: string; sort: string; index: string; } & ({ type: 'LIST'; fields: string[]; } | { type: 'CALENDAR'; date: string; title: string; } | { type: 'CUSTOM'; html: string; pager: boolean; device: 'DESKTOP' | 'ANY'; }))[]; revision: string; }> } - Retrieved views and revision number
 */
const getViews = (params) => {
  const query = new URLSearchParams(params).toString();
  const url = `https://__your_subdomain__.cybozu.com/k/v1/app/views.json?${query}`;
  const options = {
    method: 'GET',
    headers: {
      'X-Cybozu-API-Token': 'YOUR_API_TOKEN',
    },
  };
  const response = UrlFetchApp.fetch(url, options);
  return JSON.parse(response.getContentText());
};
#kintone #JavaScript #TypeScript