Forum Discussion

powerbi_jenhen's avatar
powerbi_jenhen
Resolver II
4 years ago
Solved

Power BI Rest API to Update Workspace App

Hi,   I'm using the Power BI Rest API via Powershell within ADO pipelines to fully automate the deployment of artifacts (datasets, reports, dashboards) across DEV->ACC->PROD Power BI Service worksp...
  • powerbi_jenhen's avatar
    4 years ago

    Figured this out, simply add the below to the body. The app will initially need to be published for this to work:

     

    updateAppSettings = @{
                        updateAppInTargetWorkspace = $TRUE
                    }
     
    Typical full body:
     
    $body =
                @{
                    sourceStageOrder = $stageOrder
                    datasets = @(
                        @{sourceId = $datasetArtifact.artifactId }
                    )
                    reports = @(
                        @{sourceId = $reportArtifact.artifactId }
                    )        
                    options = @{
                        allowCreateArtifact = $TRUE
                        allowOverwriteArtifact = $TRUE
                    }  
                    updateAppSettings = @{
                        updateAppInTargetWorkspace = $TRUE
                    }
                } | ConvertTo-Json
  • powerbi_jenhen's avatar
    3 years ago

    Check the pipeline and app exist by passing these artifact values into the function:

     

    # Get pipelines
            $pipelines = (Invoke-PowerBIRestMethod -Url "pipelines" -Method Get | ConvertFrom-Json).value

            # Try to find the pipeline by display name
            $pipeline = $pipelines | Where-Object {$_.DisplayName -eq $pipelineName}

            Write-Host "Pipeline Name: "$pipelineName
            Write-Host "Pipeline: "$pipeline

            if(!$pipeline)
            {
                Write-Host "A pipeline with the requested name was not found"
                return
            }

            # Get apps
            $apps = (Invoke-PowerBIRestMethod -Url "admin/apps?%24top=500" -Method Get | ConvertFrom-Json).value
            #Write-Host "Apps: "$apps

            # Try to find the apps by workspace id
            $app = $apps | Where-Object {$_.workspaceId -eq $workspaceId}

            Write-Host "Workspace ID: "$workspaceId
            Write-Host "App Workspace ID: "$app

            if(!$app)
            {
                Write-Host "A app with the requested workspace id was not found"
                #return
            }
     
    Set the different deployment bodies:
     
    # Get pipeline stage artifacts
            $artifactsUrl = "pipelines/{0}/stages/{1}/artifacts" -f $pipeline.Id,$stageOrder
            $artifacts = Invoke-PowerBIRestMethod -Url $artifactsUrl  -Method Get | ConvertFrom-Json
            $ReportDisplayName = [io.path]::GetFileNameWithoutExtension($fileName)
            $reportArtifact = $artifacts.reports | Where-Object {$_.artifactDisplayName -eq $ReportDisplayName}
            $datasetArtifact = $artifacts.datasets | Where-Object {$_.artifactDisplayName -eq $ReportDisplayName}
            $reportArtifactId = $reportArtifact.artifactId
            $datasetArtifactId = $datasetArtifact.artifactId
     
    if($app)
            {
                $body =
                @{
                    sourceStageOrder = $stageOrder
                       
                    datasets = @(
                    @{sourceId = $datasetArtifactId }
                    )
                       
                    reports = @(
                        @{sourceId = $reportArtifactId }
                    )        

                    options = @{
                        # Allows creating new artifact if needed on the Test stage workspace
                        allowCreateArtifact = $TRUE

                        # Allows overwriting existing artifact if needed on the Test stage workspace
                        allowOverwriteArtifact = $TRUE
                    }    
                    updateAppSettings = @{
                        updateAppInTargetWorkspace = $TRUE
                    }          
                } | ConvertTo-Json
            }
            else
            {
                $body =
                @{
                    sourceStageOrder = $stageOrder
                       
                    datasets = @(
                    @{sourceId = $datasetArtifactId }
                    )
                       
                    reports = @(
                        @{sourceId = $reportArtifactId }
                    )        

                    options = @{
                        # Allows creating new artifact if needed on the Test stage workspace
                        allowCreateArtifact = $TRUE

                        # Allows overwriting existing artifact if needed on the Test stage workspace
                        allowOverwriteArtifact = $TRUE
                    }          
                } | ConvertTo-Json
            }