Cmdlets for SuiteCRM

Build 24.0.9060

Establishing a Connection

With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our SuiteCRM Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.

Installing and Connecting

If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.

Install-Module SuiteCRMCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module SuiteCRMCmdlets;

You can then use the Connect-SuiteCRM cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-SuiteCRM -URL 'http://mySuiteCRM.com' -User 'myUser' -Password 'myPassword'

Connecting to SuiteCRM

Requirements for connecting to SuiteCRM differ, depending on which version of the API your site is running.

SuiteCRM V4.1 API

To connect to SuiteCRM data via the V4.1 API, set these connection properties:

  • Schema: suitecrmv4.
  • Url: The URL associated with the SuiteCRM application. For example, http://suite.crm.com.
  • User: The user associated with the SuiteCRM account.
  • Password: The password associated with the user of the SuiteCRM account.

Note that retrieving SuiteCRM metadata can be expensive. We recommend that you store the metadata locally, as described in Caching Metadata.

SuiteCRM V8 API

SuiteCRM V8 uses the OAuth2 authentication standard. Before you connect to SuiteCRM V8 API, you must ensure that is it properly configured to provide the OAuth2 private and public keys. For further information, see the SuiteCRM Developer Guide API V8 setup instructions.

Authenticating to SuiteCRM V8 API

The SuiteCRM V8 API uses OAuth2.0 as its authentication mechanism, based on either of two grant types: PASSWORD and CLIENT.

Authentication via OAuth requires the Schema to be set to suitecrmv8, and the creation of a custom OAuth application, as described in Creating a Custom OAuth Application.

Client Credentials Grant

To connect to SuiteCRM V8 API, set these properties:

  • Schema: suitecrmv8.
  • AuthScheme: OAuthClient.
  • OAuthClientId: The client key returned during custom OAuth application creation. (See Creating a Custom OAuth Application.)
  • OAuthClientSecret: The client secret returned during custom OAuth application creation (See Creating a Custom OAuth Application.)
  • URL: The base URL of your SuiteCRM system. For example, https://suitecrmhost/.

Password Grant

To connect to SuiteCRM V8 API, set these properties:

  • Schema: suitecrmv8.
  • AuthScheme: OAuthPassword.
  • OAuthClientId: The client key returned during custom OAuth application creation. (See Creating a Custom OAuth Application.)
  • OAuthClientSecret: The client secret returned during custom OAuth application creation. (See Creating a Custom OAuth Application.)
  • User: The user's username.
  • Password: The user's password.
  • URL: The base URL of your SuiteCRM system. For example, https://suitecrmhost/.

Retrieving Data

The Select-SuiteCRM cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-SuiteCRM -Connection $conn -Table "Accounts" -Columns @("Name, Industry") -Where "Industry='Manufacturing'"
The Invoke-SuiteCRM cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.

Piping Cmdlet Output

The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:

Select-SuiteCRM -Connection $conn -Table Accounts -Where "Industry = 'Manufacturing'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myAccountsData.csv -NoTypeInformation

You will notice that we piped the results from Select-SuiteCRM into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.

However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:

 
PS C:\> $conn  = Connect-SuiteCRM -URL 'http://mySuiteCRM.com' -User 'myUser' -Password 'myPassword'
PS C:\> $row = Select-SuiteCRM -Connection $conn -Table "Accounts" -Columns (Name, Industry) -Where "Industry = 'Manufacturing'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "Accounts",
  "Columns":  [

  ],
  "Name":  "MyName",
  "Industry":  "MyIndustry"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-SuiteCRM -Connection $conn -Table Accounts -Where "Industry = 'Manufacturing'" | Remove-SuiteCRM

Modifying Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into SuiteCRM, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyAccountsUpdates.csv | %{
  $record = Select-SuiteCRM -Connection $conn -Table Accounts -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-SuiteCRM -Connection $conn -Table Accounts -Columns @("Name","Industry") -Values @($_.Name, $_.Industry) -Where "Id  = `'$_.Id`'"
  }else{
    Add-SuiteCRM -Connection $conn -Table Accounts -Columns @("Name","Industry") -Values @($_.Name, $_.Industry)
  }
}

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 24.0.9060