Forum Discussion

vivek-choudhary's avatar
vivek-choudhary
Regular Visitor
2 years ago

Solved : PowerBI Exporting App to Sharepoint

With the below Powershell code, you can export the PowerBI App and save it inside the Online Sharepoint Folder location. 

 

Pre-requisite: 

Install-Module -Name MicrosoftPowerBIMgmt -RequiredVersion 1.2.0

Install-Module Microsoft.Identity.Client

Login-PowerBI

 

Code: 

 

# --- SharePoint Authentication ---
$realm = "XXXX"
$principal = "YYYY"
$targetHost = "azureAAAA.sharepoint.com"
$client_id = "BB"
$client_secret = "XX"
$resource= "YYYY/azureAAAA.sharepoint.com@XXXX"
$body = "grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret&resource=$resource"

# Make the request to get the access token
$tokenResponse = Invoke-RestMethod -Uri https://accounts.accesscontrol.windows.net/$realm/tokens/OAuth/2 -Method Post -Body $body -ContentType "application/x-www-form-urlencoded"

# Get the access token from the response
$access_token = $tokenResponse.access_token
$headers = @{
"Authorization" = "Bearer $access_token"
"Content-Type" = "application/json" # Assuming JSON content for the file
}

# --- Power BI Report Export ---
$reportId = "325CCCC"
$fileName = "Sales.pbix" # Set the desired filename for the exported report

# Export the report to a temporary file . This optional step 
#Export-PowerBIReport -Id $reportId -OutFile $fileName

# --- Upload to SharePoint ---
$url = "https://$targetHost/sites/SubsiteKK/_api/web/GetFolderByServerRelativeUrl('Shared Documents/JJJ')/files/add(url='$fileName',overwrite=true)"

# Upload the exported report to SharePoint
Invoke-RestMethod -Uri $url -Method Post -Headers $headers -InFile $fileName

1 Reply