3

I try to refresh a Power BI Premium dataset programmatically by sending post request to PBI API endpoint: datasets//refreshes.

The documentation (https://learn.microsoft.com/en-us/power-bi/connect-data/asynchronous-refresh) states: The response also includes a location response-header field to point the caller to the refresh operation that was just created/accepted. Location is that of the new resource which was created by the request, which includes the refreshId.

I need the refreshId in order to poll its status to determine whether it has succeeded.

I used the following Powershell code to refresh the dataset.

Please let me know how I can return the location field in the response header.

Login-PowerBI
$XmlaQuery = @"
{
  "refresh": {
    "type": "full",
    "objects": [
      {
        "database": "<Datamodel>",
        "table": "<Table>"
      }
    ]
  }
}
"@

# URL is a relative or absolute URL of the Power BI entity to access. 
Invoke-PowerBIRestMethod -Url 'datasets/<datasetid>/refreshes' -Method Post -Body $XmlaQuery 

I figured that Invoke-PowerBIRestMethod does not have the header property. I tried the same using Invoke-WebRequest and included the response.


$XmlaQuery = @"
{
  "refresh": {
    "type": "full",
    "objects": [
      {
        "database": "<Datamodel>",
        "table": "<Table>"
      }
    ]
  }
}
"@

# URL is a relative or absolute URL of the Power BI entity to access.
Login-PowerBIServiceAccount  
$headers = Get-PowerBIAccessToken
$Response = Invoke-WebRequest  -Uri 'https://api.powerbi.com/v1.0/myorg/groups/<workspaceid>/datasets/<datasetid>/refreshes' -Method Post -Body $XmlaQuery -Headers $headers
$Response.Headers | Format-Table
$Response.StatusCode

Response (with status code 202):

Key                           Value
---                           -----
Pragma                        no-cache
Transfer-Encoding             chunked
Strict-Transport-Security     max-age=31536000; includeSubDomains
X-Frame-Options               deny
X-Content-Type-Options        nosniff
RequestId                     <RequestId>
Access-Control-Expose-Headers RequestId
request-redirected            true
home-cluster-uri              https://wabi-north-europe-f-primary-redirect.analysis.windows.net/
Cache-Control                 no-store, must-revalidate, no-cache
Content-Type                  application/octet-stream
Date                          Mon, 17 Jan 2022 16:09:30 GMT
Luukv93
  • 339
  • 1
  • 6
  • 19
  • You'll first need to capture the response. What does `Invoke-PowerBIRestMethod` return? If nothing useful, I see that there is a parameter `-Outfile` for capturing the response to a file – Nick.Mc Jan 17 '22 at 13:38
  • Hi Nick, unfortunately the response headers don't include the location key. Specifying -Outfile creates a blank file... Also worth mentioning is that the request returns a 200 instead of a 202 Accepted – Luukv93 Jan 17 '22 at 13:49
  • It might help to post an example of the response in the question. – Nick.Mc Jan 17 '22 at 14:03
  • Updated my post, used the Invoke-WebRequest to return the response headers. As you can see there is no location field... – Luukv93 Jan 17 '22 at 16:17
  • 1
    Out of curiosity - does the response body contain anything? – Tomalak Jan 17 '22 at 16:22
  • @Tomalak there is no response body (I assume I could access response body by $Response.Body ? ) – Luukv93 Jan 17 '22 at 16:40
  • I don't expect it for a 202 status, I'm just checking because apparently there is a `Content-Type` header, which implies that there is some sort of content. For regular HTTP responses you could look at `$response.Content`. – Tomalak Jan 17 '22 at 16:49
  • @Tomalak it doesn’t return anything.. – Luukv93 Jan 17 '22 at 19:12
  • @Luukv93, did you find the solution? I’m facing exactly same issue using .NET code, working from Postman (status code 202 accepted + requestId returned) but from my code, it’s returning 200 OK with no requestId – Valouf May 19 '22 at 07:12

2 Answers2

0

Perhaps try removing the "refresh" layer in the request body. From the public documentation, it doesn't seem to contain this layer. And also setup "Content-Type: application/json" in the request header.

jastlu
  • 1
  • 1
0

After you POST refresh, why do you just not check if is there any new requestID from GET method?

GET https://api.powerbi.com/v1.0/myorg/datasets/{datasetId}/refreshes?$top={$top}

there should be a "refreshType": "ViaApi" and StartTime, and Status "inProgress"/"Completed"/"not started"

{
  "value": [
    {
      "refreshType": "ViaApi",
      "startTime": "2017-06-13T09:25:43.153Z",
      "endTime": "2017-06-13T09:31:43.153Z",
      "status": "Completed",
      "requestId": "9399bb89-25d1-44f8-8576-136d7e9014b1"
    }
  ]
}
msta42a
  • 3,601
  • 1
  • 4
  • 14
  • 1
    I understand and this is our current implementation. I just need to understand why the refreshId is not returned. – Luukv93 Jan 30 '22 at 19:14