Cmdlets for Smartsheet

Build 24.0.8963

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 Smartsheet 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 SmartsheetCmdlets

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

Import-Module SmartsheetCmdlets;

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

$conn = Connect-Smartsheet -OAuthClientId 'MyOAuthClientId' -OAuthClientSecret 'MyOAuthClientSecret' -CallbackURL 'http://localhost:33333'

Connecting to Smartsheet

Smartsheet supports connections via the following authentication methods:

  • Using the Personal Access Token

Personal Access Token

Use the personal token to test and to access your own data. To obtain the personal token, follow the steps below:

  1. Log into Smartsheet.
  2. Click Account and select Personal Settings.
  3. Click API Access and use the form to generate new access tokens or manage existing access tokens.
Set the AuthScheme to PersonalAccessToken. You can then set the PersonalAccessToken to the token you generated.

Synch Connections

Before you connect, set the following variables:

  • OAuthClientId: The client Id assigned when you registered your custom OAuth application.
  • OAuthClientSecret: The client secret assigned when you registered your custom OAuth application.

Click Connect to Smartsheet to open the OAuth endpoint in your default browser. Log in and grant permissions to the application.

The driver then completes the OAuth process as follows:

  • Extracts the access token from the callback URL.
  • Obtains a new access token when the old one expires.
  • Saves OAuth values so that they persist across connections.

Retrieving Data

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

$results = Select-Smartsheet -Connection $conn -Table "Sheet_Test_Sheet" -Columns @("Id, Name") -Where "Favorite='True'"
The Invoke-Smartsheet 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-Smartsheet -Connection $conn -Table Sheet_Test_Sheet -Where "Favorite = 'True'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\mySheet_Test_SheetData.csv -NoTypeInformation

You will notice that we piped the results from Select-Smartsheet 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-Smartsheet -OAuthClientId 'MyOAuthClientId' -OAuthClientSecret 'MyOAuthClientSecret' -CallbackURL 'http://localhost:33333'
PS C:\> $row = Select-Smartsheet -Connection $conn -Table "Sheet_Test_Sheet" -Columns (Id, Name) -Where "Favorite = 'True'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "Sheet_Test_Sheet",
  "Columns":  [

  ],
  "Id":  "MyId",
  "Name":  "MyName"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-Smartsheet -Connection $conn -Table Sheet_Test_Sheet -Where "Favorite = 'True'" | Remove-Smartsheet

Modifying Data

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

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

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