cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
BenTheSmaller
Regular Visitor

REST import rdl files

Hi,

 

I'm working on an automated deployment solution in powershell to import to PowerBI using the REST API.

 

I have borrowed the function at this URL and am able to import pbix files without any problems:

 

https://github.com/Microsoft/powerbi-powershell/issues/46

 

function Publish-PowerBIImport {
    param
    (
        [string]$Path,
        [string]$GroupId
    )

    $powerBiBodyTemplate = @'
--{0}
Content-Disposition: form-data; name="fileData"; filename="{1}"
Content-Type: application/x-zip-compressed

{2}
--{0}--

'@

    $fileName = [IO.Path]::GetFileName($Path)
    $boundary = [guid]::NewGuid().ToString()
    $fileBytes = [System.IO.File]::ReadAllBytes($Path)
    $encoding = [System.Text.Encoding]::GetEncoding("iso-8859-1")
    $filebody = $encoding.GetString($fileBytes)
    $body = $powerBiBodyTemplate -f $boundary, $fileName, $encoding.GetString($fileBytes)

    $headers = Get-PowerBIAccessToken

    if ($GroupId) {
        $url = "https://api.powerbi.com/v1.0/myorg/groups/$GroupId/imports?datasetDisplayName=$fileName&nameConflict=CreateOrOverwrite"
    }
    else {
        $url = "https://api.powerbi.com/v1.0/myorg/imports?datasetDisplayName=$fileName&nameConflict=CreateOrOverwrite"
    }

    Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body -ContentType "multipart/form-data; boundary=--$boundary" | Out-Null
    Write-Host "Imported Power BI file $Path"
}

 

However when I feed this function a rdl file, I get the following error:


Invoke-RestMethod :
{"error":{"code":"ImportUnsupportedOptionError","pbi.error":{"code":"ImportUnsupportedOptionError","parameters":{},"details":[],"exceptionCulprit":1}}}
At G:\Visual Studio Projects\PowerShell Scripts\PowerBIDeployment.ps1:34 char:5
+ Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

I have modified the Content-Type header and the encoding to suit the rdl file, as below

 

function Publish-PowerBIImport {
    param
    (
        [string]$Path,
        [string]$GroupId
    )

    $powerBiBodyTemplate = @'
--{0}
Content-Disposition: form-data; name="fileData"; filename="{1}"
Content-Type: text/xml

{2}
--{0}--

'@

    $fileName = [IO.Path]::GetFileName($Path)
    $boundary = [guid]::NewGuid().ToString()
    $fileBytes = [System.IO.File]::ReadAllBytes($Path)
    $encoding = [System.Text.Encoding]::GetEncoding("utf-8")
    $filebody = $encoding.GetString($fileBytes)
    $body = $powerBiBodyTemplate -f $boundary, $fileName, $encoding.GetString($fileBytes)

    $headers = Get-PowerBIAccessToken

    if ($GroupId) {
        $url = "https://api.powerbi.com/v1.0/myorg/groups/$GroupId/imports?datasetDisplayName=$fileName&nameConflict=CreateOrOverwrite"
    }
    else {
        $url = "https://api.powerbi.com/v1.0/myorg/imports?datasetDisplayName=$fileName&nameConflict=CreateOrOverwrite"
    }

    Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body -ContentType "multipart/form-data; boundary=--$boundary" | Out-Null
    Write-Host "Imported Power BI file $Path"
}

The error code suggests rdl import may not yet be supported in this manner, can anyone confirm/deny? Cheers.

2 REPLIES 2
TeresaL
New Member

The problem with your code is the value of the nameConflict parameter:

 

$url = "https://api.powerbi.com/v1.0/myorg/groups/$GroupId/imports?datasetDisplayName=$fileName&nameConflict=CreateOrOverwrite"

For RDL files, the only supported values are Abort or Overwrite.

There is, however, a bug in that we are not returning a helpful error message.  We are looking in to fixing this in a future release.

 

Thanks,

Teresa (MSFT)

Anonymous
Not applicable

Hi All,

 

I am trying to upload  a rdl file to a workspace programatically through Power BI rest API in a .net solution. We are using Power BI .net SDK to acheive this functionality. We are using "PostImportWithFileAsyncInGroup" method to upload the rdl file but getting "Badrequest" error all the time. I  have verified that the capacity has been set "A4" and the Paginated workload is also turned on. It would be really helpful if somebody can shed some light on this issue.

Thanks.

Helpful resources

Announcements
Power BI December 2021 Update_carousel 768x460.jpg

Check it Out!

Click here to read more about the December 2021 Updates!

User Group Leader Meeting January 768x460.png

Calling all User Group Leaders!

Don't miss the User Group Leader meetings on January, 24th & 25th, 2022.

Jan 2022 Dev Camp 768x460 copy.png

Power BI Dev Camp- January 27th, 2022

Mark your calendars and join us for our next Power BI Dev Camp!

Top Solution Authors
Top Kudoed Authors