Question

Getting data from web API using either DataService or Odata or other option using PowerShell?

Has anyone been able to use PowerShell to call bmp'online's Rest API to get data?  any examples of the JSON body contents of the GET request?  I just need to read a few fields on a specific account for an external integration tool.

Thanks.

Like 0

Like

3 comments
Best reply

Here's the solution...

########################################################################
# Example of how to read data from bpm'online via ODATA using PowerShell
# Written by Brian Morgan, May 17, 2018
########################################################################
 
#----------------------------------------------------------------------
#auth code
#----------------------------------------------------------------------
$URL = 'https://<your-domain>.bpmonline.com/ServiceModel/AuthService.svc/Login'
$body = '{ "UserName": "<your-username>", "UserPassword": "<your-password>" }'
$void = Invoke-RestMethod -Uri $URL -Body $body -Method Post `
        -ContentType 'application/json' -SessionVariable CookieJar
 
# NOTE the CookieJar variable, this handles all the cookie stuff so easily 
# I LOVE POWERSHELL!
# This code Read accounts data, gets the first 40 by default. 
# NOTE the ";odata=verbose"- this is CRITICAL on the Accept header!!!!!!
 
$URL = 'https://<your-domain>.bpmonline.com/0/ServiceModel/EntityDataService.svc/AccountCollection'
#----------------------------------------------------------------------
# Read the data
#----------------------------------------------------------------------
$data = Invoke-RestMethod -Uri $URL -Method Get -Headers @{'Accept'='application/json;odata=verbose'} `
        -WebSession $CookieJar
 
# then you can loop through each account and show Name and Phone as example
 
foreach ($acc in $data.d.results) {
    $acc.Name
    $acc.Phone
}
 
#----------------------------------------------------------------------
# You can update data by getting the ID from the account
#  This is JSON format, the easiest to do!
#----------------------------------------------------------------------
 
$guid = $acc.Id
$body = @"
{
    "Name": "this is the new name"
}
"@
 
$URL = "https://<your-domain>.bpmonline.com/0/ServiceModel/EntityDataService.svc/AccountCollection(guid'$guid')"
 
Invoke-RestMethod -Uri $URL -Method Put `
        -body $body -ContentType 'application/json;odata=verbose' -WebSession $CookieJar
 
#NOTE: the "application/json;odata=verbose",  this is the MAGIC to specify JSON!!!!!!

 so easy IF you add the magic phrase of odata=verbose to the Accept header

Please find the requests that you need to build in the article by the link below. 

https://academy.bpmonline.com/documents/technic-sdk/7-11/executing-odat…

If you need to read data, then you need to create a login request, receive login cookies and then create a get request with the received cookies.

Please find how to create a get request with PowerShell in the microsoft documentation by the link below

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell…

https://www.jokecamp.com/blog/invoke-restmethod-powershell-examples/

Here's the solution...

########################################################################
# Example of how to read data from bpm'online via ODATA using PowerShell
# Written by Brian Morgan, May 17, 2018
########################################################################
 
#----------------------------------------------------------------------
#auth code
#----------------------------------------------------------------------
$URL = 'https://<your-domain>.bpmonline.com/ServiceModel/AuthService.svc/Login'
$body = '{ "UserName": "<your-username>", "UserPassword": "<your-password>" }'
$void = Invoke-RestMethod -Uri $URL -Body $body -Method Post `
        -ContentType 'application/json' -SessionVariable CookieJar
 
# NOTE the CookieJar variable, this handles all the cookie stuff so easily 
# I LOVE POWERSHELL!
# This code Read accounts data, gets the first 40 by default. 
# NOTE the ";odata=verbose"- this is CRITICAL on the Accept header!!!!!!
 
$URL = 'https://<your-domain>.bpmonline.com/0/ServiceModel/EntityDataService.svc/AccountCollection'
#----------------------------------------------------------------------
# Read the data
#----------------------------------------------------------------------
$data = Invoke-RestMethod -Uri $URL -Method Get -Headers @{'Accept'='application/json;odata=verbose'} `
        -WebSession $CookieJar
 
# then you can loop through each account and show Name and Phone as example
 
foreach ($acc in $data.d.results) {
    $acc.Name
    $acc.Phone
}
 
#----------------------------------------------------------------------
# You can update data by getting the ID from the account
#  This is JSON format, the easiest to do!
#----------------------------------------------------------------------
 
$guid = $acc.Id
$body = @"
{
    "Name": "this is the new name"
}
"@
 
$URL = "https://<your-domain>.bpmonline.com/0/ServiceModel/EntityDataService.svc/AccountCollection(guid'$guid')"
 
Invoke-RestMethod -Uri $URL -Method Put `
        -body $body -ContentType 'application/json;odata=verbose' -WebSession $CookieJar
 
#NOTE: the "application/json;odata=verbose",  this is the MAGIC to specify JSON!!!!!!

 so easy IF you add the magic phrase of odata=verbose to the Accept header

ODATA JSON - you specify that it's JSON by using 'application/json;odata=verbose" or it does NOT work with the API!!!  You must use it in the Accept header for GET and in the ContentType with PUT/POST.  Enjoy.

Show all comments