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 Cvent 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 CventCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module CventCmdlets;
You can then use the Connect-Cvent cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-Cvent -InitiateOAuth "GETANDREFRESH" -OAuthClientId "MyOAuthClientId" -OAuthClientSecret "MyOAuthClientSecret"
The Cvent REST API uses the client credentials flow of OAuth 2.0 to authorize requests to the Cvent.
Before You Connect
Before you can authenticate to Cvent, you must create a workspace and an OAuth application.
Creating a Workspace
To create a workspace:- Sign into Cvent and navigate to App Switcher (the blue button in the upper right corner of the page) > Admin.
- In the Admin menu, navigate to Integrations > REST API.
- A new tab launches for Developer Management. Click on Manage API Access in the new tab.
- Create a Workspace and name it. Select the scopes you would like your developers to have access to. Scopes control what data domains the developer can access.
- Choose All to allow developers to choose any scope, and any future scopes added to the REST API.
- Choose Custom to limit the scopes developers can choose for their OAuth apps to selected scopes. To access all tables exposed by the cmdlet, you need to set the following scopes:
- event/attendees:read
- event/attendees:write
- event/contacts:read
- event/contacts:write
- event/custom-fields:read
- event/custom-fields:write
- event/events:read
- event/events:write
- event/sessions:delete
- event/sessions:read
- event/sessions:write
- event/speakers:delete
- event/speakers:read
- event/speakers:write
- budget/budget-items:read
- budget/budget-items:write
- exhibitor/exhibitors:read
- exhibitor/exhibitors:write
- survey/surveys:read
- survey/surveys:write
Creating an OAuth Application
After you have set up a Workspace and invited them, developers can sign up and create a custom OAuth app. See Creating a Custom OAuth Application for a procedure.Connecting to Cvent
After creating an OAuth application, set the following connection properties to connect to Cvent:
- InitiateOAuth: GETANDREFRESH. Used to automatically get and refresh the OAuthAccessToken.
- OAuthClientId: The Client ID associated with the OAuth application. You can find this on the Applications page in the Developer Portal.
- OAuthClientSecret: The Client secret associated with the OAuth application. You can find this on the Applications page in the Developer Portal.
Retrieving Data
The Select-Cvent cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-Cvent -Connection $conn -Table "Events" -Columns @("Id, Title") -Where "Title='CDATA_EVENT'"The Invoke-Cvent 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-Cvent -Connection $conn -Table Events -Where "Title = 'CDATA_EVENT'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myEventsData.csv -NoTypeInformation
You will notice that we piped the results from Select-Cvent 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-Cvent -InitiateOAuth "GETANDREFRESH" -OAuthClientId "MyOAuthClientId" -OAuthClientSecret "MyOAuthClientSecret" PS C:\> $row = Select-Cvent -Connection $conn -Table "Events" -Columns (Id, Title) -Where "Title = 'CDATA_EVENT'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Events", "Columns": [ ], "Id": "MyId", "Title": "MyTitle" }
Deleting Data
The following line deletes any records that match the criteria:
Select-Cvent -Connection $conn -Table Events -Where "Title = 'CDATA_EVENT'" | Remove-Cvent
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Cvent, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyEventsUpdates.csv | %{ $record = Select-Cvent -Connection $conn -Table Events -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-Cvent -Connection $conn -Table Events -Columns @("Id","Title") -Values @($_.Id, $_.Title) -Where "Id = `'$_.Id`'" }else{ Add-Cvent -Connection $conn -Table Events -Columns @("Id","Title") -Values @($_.Id, $_.Title) } }