Posting Data to the Creatio OData REST API from PowerShell

Creatio’s OData REST API is easy to use. However, it is typically a multi-step process to first login, then do your actual request. Also, there are items from the login request that must be added to subsequent requests (cookies and adding the BPMCSRF cookie value returned from the login to a BPMCSRF header). This article will show you how to do this in a PowerShell script.

Overview of the Steps:

  1. POST to https://BaseUrl/ServiceModel/AuthService.svc/Login
  2. Get cookies from result
  3. Read value of BPMCSRF cookie
  4. POST contact data to https://BaseUrl/0/odata/Contact
  5. Copy BPMCSRF value from auth response as a header with name BPMCSRF
  6. Copy cookies from auth response

Here’s a complete sample that logs in and creates a contact

########################################################################
# CREATIO ODATA - CREATE CONTACT
# From Customer FX - customerfx.com
########################################################################

# Creatio details
$baseUrl = "https://YourAddressHere.creatio.com/"
$userName = "UserNameHere"
$password = "PasswordHere"


#----------------------------------------------------------------------
# Authorize request
#----------------------------------------------------------------------

Write-Host "Logging into Creatio"

$uri = $baseUrl + "/ServiceModel/AuthService.svc/Login"
$body = @{
	UserName = $userName
	UserPassword = $password
} | ConvertTo-Json -Compress

try {
	$result = Invoke-RestMethod -Uri $uri -Body $body -Method POST -ContentType 'application/json' -SessionVariable websession
}
catch {
	Write-Host "Error logging in: (" $_.Exception.Response.StatusCode.value__ ") " $_.Exception.Response.StatusDescription
	Exit
}

if ($result.Code -ne 0) {
	Write-Host "Error logging in: " $result.Message
	Exit
}

Write-Host "Creatio login successful"

# Get values for subsequent requests
$cookies = $websession.Cookies.GetCookies($uri) 
foreach ($cookie in $cookies) {
	if ($cookie.name -eq "BPMCSRF") {
		$bpmcsrf = $cookie.value
	}
}
 
#----------------------------------------------------------------------
# Create contact
#----------------------------------------------------------------------

$contactName = "Test Contact"
$contactEmail = "test@test.com"

$uri = $baseUrl + "0/odata/Contact"
$headers = @{
	"Content-Type" = "application/json;odata=verbose"
	"ForceUseSession" = "true"
	"BPMCSRF" = $bpmcsrf
}

# Add any columns to set for the contact record here
$body = @{
    Name = $contactName
	Email = $contactEmail
} | ConvertTo-Json -Compress

$params = @{
	Uri = $uri
	Method = "POST"
	Headers = $headers
	Body = $body
	WebSession = $websession
}

Write-Host "Creating contact"

try {
	$result = Invoke-RestMethod @params
}
catch {
	Write-Host "Error creating contact: (" $_.Exception.Response.StatusCode.value__ ") " $_.Exception.Response.StatusDescription
	Exit
}

Write-Host "Contact created sucessfully. Created contact Id: " $result.Id
# End

Note, you can easily read the returned contact data from the result, for example, $result.CreatedOn etc. The above could be modified to read record using GET or update using PATCH to /0/odata/Contact(SomeContactId)

Want content like this delivered to your inbox? Sign up for our newsletter!
ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

Submit a Comment

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