Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
dkanman
New Member

Post a Dataflow JSON to a specified workspace in powerbi online

Hi All,

 

I am trying to use the REST API to post a dataflow JSON to a specified workspace on powerbi online using authorization token and workspace ID. Below is the code I am trying to use, however I am getting an ImportSizeErrorCode with exceptionCulprit: 1. Does anyone have advice on accomplishing this? it works in python however I need it to be in Powershell.

function Read-JsonFromFile {
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$FilePath
)

# Read the file contents
$fileContent = Get-Content -Path $FilePath -Raw

# Convert the file content to JSON
try {
$jsonObject = $fileContent | ConvertFrom-Json -AsHashtable
return $jsonObject
}
catch {
Write-Error "Failed to parse JSON from file: $FilePath"
return $null
}
}
$jsonFile = Read-JsonFromFile -FilePath "DNAV-GLOBAL-DEMO-FUND-AccountBalance.json"

Write-Output $jsonObject

 


function Remove-Partitions {
param (
[Parameter(Mandatory = $true)]
[System.Collections.Hashtable]$JsonFile
)

for ($i = 0; $i -lt $JsonFile["entities"].Count; $i++) {
if ($JsonFile["entities"][$i].ContainsKey("partitions")) {
$JsonFile["entities"][$i].Remove("partitions")
}
else {
Write-Host "Key not found in JSON data."
}
}

Write-Host "Partitions Removed"
return $JsonFile
}

$jsonFile = Remove-Partitions -JsonFile $jsonFile

 

function to-powerbi {
param (
[Parameter(Mandatory=$true)]
[string]$json_file,
[Parameter(Mandatory=$true)]
[string]$file_name,
[Parameter(Mandatory=$true)]
[string]$access_token,
[Parameter(Mandatory=$true)]
[string]$group_id
)

# Prepare the multipart/form-data request body
$json_string = ConvertTo-Json $json_file -Depth 5
$multipart_data = @{
"model.json" = @{
"filename" = (Split-Path -Leaf $file_name)
"content" = $json_string
"content-type" = "application/json"
}
}

# Set the headers
$headers = @{
"Authorization" = "Bearer $access_token"
"Content-Type" = "multipart/form-data"
}

# Set export URL
$export_url = "https://api.powerbi.com/v1.0/myorg/groups/$group_id/imports?datasetDisplayName=$file_name"
$response = Invoke-RestMethod -Uri $export_url -Method Post -Headers $headers -Body $multipart_data

# Check response
if ($response.StatusCode -eq 202) {
Write-Host "Dataflow import successful"
Write-Host "Dataflow posted to PowerBI as $file_name"
} else {
Write-Host "Error: $($response | Select-Object StatusCode) $($response | ConvertTo-Json -Depth 10)"
}
}

 

to-powerbi -json_file $jsonFile -file_name $file_name -access_token $AuthToken -group_id $workspace_id

 

1 ACCEPTED SOLUTION
dkanman
New Member

I ended up finding the solution, it is to use the built in powershell multipart encoding capabilities under System.Net.Http.HttpClient

View solution in original post

1 REPLY 1
dkanman
New Member

I ended up finding the solution, it is to use the built in powershell multipart encoding capabilities under System.Net.Http.HttpClient

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.