How to access the Fields plugin data with the GLPI API

I needed to find out how to request the API to retrieve data from the Fields plugin. I thought it might be useful to share how to do.

Reminder : basic command

For example, a basic command path to get data from one or several computers can be :

[GLPI_URI]/apirest.php/computer/[COMPUTER_ID]
[GLPI_URI]/apirest.php/computer/?range=0-1000

Finaly, the complete command line could be :

curl -X GET \
-H 'Content-Type: application/json' \
-H "Session-Token: [SESSION_TOKEN]" \
-H "App-Token: [APP_TOKEN]" \
'[GLPI_URI]/apirest.php/computer/?range=0-1000'
  • [SESSION_TOKEN] : token given by connection initialisation
  • [APP_TOKEN] : token generated in GLPI (Setup>General>Api Clients)

To learn the first steps for initialising connection with GLPI API, see this article : GLPI : Initializing API Connection

Set the command line dedicated to custom fields

To get data from the plugin Fields, things are a bit different. Regarding to the basic command for computers, “computer” (the bold part in the command line example above) have to be replaced by something else. For that, you have to search into the plugin. Different ways are offered. Choose your own.

Before starting to search, 2 elements have to be noticed for the next steps

  • [ITEM_TYPE] : the item on which is attached the custom fields
  • [FIELD_TAB] : the tab created to host the custom fields in the item

For example, if you create a tab named “So Many Great Things” (the FIELD_TAB) for the item “Location” (the ITEM_TYPE)

The name of the custom item

You have 2 choises. It may depends on whether you have access to the code or the database.

Get the exact name of the item from with the PHP Class name

Search in that path :

[GLPI_PATH]/files/_plugins/fields/inc/[...].class.php

Replacing [...] with [ITEM_TYPE][FIELD_TAB] :

  • concatenated
  • lowercase
  • without spaces

With our example, we get : locationsomanygreatthings.

In that file, the name of the class will give you the name of the item to use to call the API :

<?php

class PluginFieldsLocationsomanygreatthings extends CommonDBTM
{

With our example, we should get the name PluginFieldsLocationsomanygreatthings and use the following URI :

[GLPI_URI]/apirest.php/computer/PluginFieldsLocationsomanygreatthings/[LOCATION_ID]
[GLPI_URI]/apirest.php/computer/PluginFieldsLocationsomanygreatthings/?range=0-1000

Deduce the name of the item from the Database Table name

In the same way of the previous solution, getting a name with [ITEM_TYPE][FIELD_TAB] in lowercase without spaces, you should find a table which the name beggins with glpi_plugin_fields_[ITEM_TYPE][FIELD_TAB].

You can verify searching with the following request :

SHOW TABLES LIKE 'glpi_plugin_fields_%';

Finaly, if you have never renamed the tab, the item should be name CustomField[ITEM_TYPE][FIELD_TAB] with [ITEM_TYPE][FIELD_TAB] in lowercase and only the first letter of [ITEM_TYPE] in uppercase.

Caution

The item name (PHP class or database table) will not have been changed if the name (in fields management interface) has been changed in the meantime.

I saw that case after correcting a spelling error on a tab name. The spelling mistake is forever stored… and will have to be used with the Client API.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *