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 workspaces. I use Power BI Service deployment pipelines but automate this very manual deployment process via "Selective Deploy" API calls within the ADO pipelines along with governance processes like archiving the artifacts incase of rollback etc. The one thing missing to fully automate the end to end process is to update the workspace apps once the artifacts have been deployed, I can't seem to find this API call, can this be done within the POST body of the Invoke-PowerBIRestMethod?

 

Thanks.

  • 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
  • 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
            }

8 Replies

  • 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
    • Anonymous's avatar
      Anonymous
      Not applicable

      I have a similar problem: I want to update a workspace app via Powershell. But I do not use Pipelines. What would the skript look like? Thanks a lot in advance.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Did you figure out if the power bi or fabric api support this? 

         

    • kerski's avatar
      kerski
      Advocate II

      What API endpoint did you do this with?

      • powerbi_jenhen's avatar
        powerbi_jenhen
        Resolver II
        $url = "pipelines/{0}/Deploy" -f $pipeline.Id
         
        $body = @{
                    sourceStageOrder = $stageOrder
                       
                    reports = @(
                        @{sourceId = $rdlArtifactId }
                    )        

                    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
         
        $deployResult = Invoke-PowerBIRestMethod -Url $url -Method Post -Body $body | ConvertFrom-Json
  • 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
            }
  • Has anyone tried to get this API working to Update or Publish the app ?  I'm curious if it only works for Premium since it uses Deployment pipelines?