cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
rollosb
Frequent Visitor

Problems importing PBIX by means of REST APIs

Disclaimer: I'm very new to Power BI and PowerShell!

 

I have been trying to import a report .pbix file to a workspace, by following the very detailed tutorial given here by Sirui Sun. To do so, I've been attempting to understand the following piece of code.  The .pbix file has to be encoded as form data, as explained in the reference for the create import API.

 

try {
        "== Importing $report_name to target workspace"
        $uri = "https://api.powerbi.com/v1.0/$target_group_path/imports/?datasetDisplayName=$report_name.pbix&nameConflict=Abort"

        # Here we switch to HttpClient class to help POST the form data for importing PBIX
        $httpClient = New-Object System.Net.Http.Httpclient $httpClientHandler
        $httpClient.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $token.AccessToken);
        $packageFileStream = New-Object System.IO.FileStream @($temp_path, [System.IO.FileMode]::Open)
        
	    $contentDispositionHeaderValue = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data"
	    $contentDispositionHeaderValue.Name = "file0"
	    $contentDispositionHeaderValue.FileName = $file_name
 
        $streamContent = New-Object System.Net.Http.StreamContent $packageFileStream
        $streamContent.Headers.ContentDisposition = $contentDispositionHeaderValue
        
        $content = New-Object System.Net.Http.MultipartFormDataContent
        $content.Add($streamContent)

	    $response = $httpClient.PostAsync($Uri, $content).Result
 
	    if (!$response.IsSuccessStatusCode) {
		    $responseBody = $response.Content.ReadAsStringAsync().Result
            "= This report cannot be imported to target workspace. Skipping..."
			$errorMessage = "Status code {0}. Reason {1}. Server reported the following message: {2}." -f $response.StatusCode, $response.ReasonPhrase, $responseBody
			throw [System.Net.Http.HttpRequestException] $errorMessage
		} 
        
        # save the import IDs
        $import_job_id = (ConvertFrom-JSON($response.Content.ReadAsStringAsync().Result)).id

        # wait for import to complete
        $upload_in_progress = $true
        while($upload_in_progress) {
            $uri = "https://api.powerbi.com/v1.0/$target_group_path/imports/$import_job_id"
            $response = Invoke-RestMethod -Uri $uri –Headers $auth_header –Method GET
            
            if ($response.importState -eq "Succeeded") {
                "Publish succeeded!"
                # update the report and dataset mappings
                $report_id_mapping[$report_id] = $response.reports[0].id
                $dataset_id_mapping[$dataset_id] = $response.datasets[0].id
                break
            }

            if ($response.importState -ne "Publishing") {
                "Error: publishing failed, skipping this. More details: "
                $response
                break
            }
            
            Write-Host -NoNewLine "."
            Start-Sleep -s 5
        }
            
        
    } catch [Exception] {
        Write-Host $_.Exception
	    Write-Host "== Error: failed to import PBIX"
        Write-Host "= HTTP Status Code:" $_.Exception.Response.StatusCode.value__ 
        Write-Host "= HTTP Status Description:" $_.Exception.Response.StatusDescription
        continue
    }

However, my pbix file is not bound to a dataset (I have multiple reports bound to one dataset as described here), and I constantly get a 400 Bad Request error, along with the error "PowerBIModelNotFoundException". As the dataset is already available in the workspace, I'm looking for a means to bind the report to the data set during the import. Is that possible?

 

Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
rollosb
Frequent Visitor

Re: Problems importing PBIX by means of REST APIs

I cracked it by finally figuring out where I had to look on this board, also taking my inspiration from here. As some of you might know by now, the problem was the Connections file in the pbix zip archive that had to be altered, so that it would containsinformation about the dataset that the report is supposed to be connected to.

 

Looking into the structure of the connections file, it is nothing more than a hashtable converted to JSON, and if one wishes, the connection can be easily modified by renaming the pbix file to a zip file, unzipping, rewriting the connections file, zipping and renaming again.

View solution in original post

4 REPLIES 4
rollosb
Frequent Visitor

Re: Problems importing PBIX by means of REST APIs

I cracked it by finally figuring out where I had to look on this board, also taking my inspiration from here. As some of you might know by now, the problem was the Connections file in the pbix zip archive that had to be altered, so that it would containsinformation about the dataset that the report is supposed to be connected to.

 

Looking into the structure of the connections file, it is nothing more than a hashtable converted to JSON, and if one wishes, the connection can be easily modified by renaming the pbix file to a zip file, unzipping, rewriting the connections file, zipping and renaming again.

View solution in original post

Microsoft Eric_Zhang
Microsoft

Re: Problems importing PBIX by means of REST APIs


@rollosb wrote:

I figured it out by finally figuring out what I had to look for on this board, taking my inspiration from here. As some of you might know by now, the problem is the Connections file in the pbix zip archive that has to be altered, so that it contains information about the dataset that the report is connected to.

 

Looking into the structure of the connections file, it is nothing more than a hashtable converted to JSON, and if one wishes, the connection can be easily modified by renaming the pbix file to a zip file, unzipping, rewriting the connections file, zipping and renaming again.


@rollosb

So you mean someone has unzipped the pbix file and changed the connection file? By no means it is supported offcially, I'd suggest avoiding such operations.

rollosb
Frequent Visitor

Re: Problems importing PBIX by means of REST APIs

@Eric_Zhang That is exactly it, and you're right - this is also mentioned in the blog post that I referred to.

I posed the question originally, because the clone API did not seem to do what I wanted. When cloning a report from one workspace to another (as described in Sirui Sun's tutorial), the clone API would incorporate the original dataset in each of the multiple reports. I can not make sense of why it does so, and I have been using the exact same code that Sirui used for that purpose.

JasonAkin
Frequent Visitor

Re: Problems importing PBIX by means of REST APIs

Im having a sililar issue. What did you have to change in the connection file?

Helpful resources

Announcements
New Ranks Launched March 24th!

New Ranks Launched March 24th!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power BI Community!

‘Better Together’ Contest Finalists Announced!

‘Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

Arun 'Triple A' Event Video, Q&A, and Slides

Arun 'Triple A' Event Video, Q&A, and Slides

Missed the Arun 'Triple A' event or want to revisit it? We've got you covered! Check out the video, Q&A, and slides now.

Top Solution Authors
Top Kudoed Authors