Forum Discussion

Suhel_Ansari's avatar
Suhel_Ansari
Helper V
11 months ago
Solved

Save the Paginated Dynamic Subscription into the SharePoint or OneDrive

Hi All, I would like to inquire about the possibility of saving the Paginated Dynamic Subscription into SharePoint or OneDrive, with the intention of making it accessible to our community. This appr...
  • tayloramy's avatar
    11 months ago

     Suhel_Ansari

     

    I ran into this same requirement and the easiest way I found to do it was to run a custom Python script to export the report. 

    Reports - Export To File - REST API (Power BI Power BI REST APIs) | Microsoft Learn

     

     Here is my function that does the export if it helps at all: 

    def export_report_pdf_with_filters(token: str, group_id: str, report_id: str,
                                       export_filter: str, outfile: pathlib.Path,
                                       include_hidden_pages: bool = False, poll_timeout_s: int = 300):
        headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
        body = {
            "format": "PDF",
            "powerBIReportConfiguration": {
                "reportLevelFilters": [{"filter": export_filter}],
                "settings": {"includeHiddenPages": include_hidden_pages}
            }
        }
        # 1) start
        start = requests.post(f"{PBI_API_BASE}/groups/{group_id}/reports/{report_id}/ExportTo",
                              headers=headers, data=json.dumps(body))
        start.raise_for_status()
        export_id = start.json()["id"]
    
        # 2) poll
        status_url = f"{PBI_API_BASE}/groups/{group_id}/reports/{report_id}/exports/{export_id}"
        t0 = time.time()
        while True:
            s = requests.get(status_url, headers=headers)
            s.raise_for_status()
            st = s.json().get("status")
            if st == "Succeeded":
                break
            if st == "Failed":
                raise RuntimeError(f"Export failed: {s.text}")
            retry_after = s.headers.get("Retry-After")
            time.sleep(int(retry_after) if retry_after and retry_after.isdigit() else 2)
            if time.time() - t0 > poll_timeout_s:
                raise TimeoutError(f"Polling timed out after {poll_timeout_s}s; last status={st}")
    
        # 3) download
        file_resp = requests.get(f"{status_url}/file", headers=headers)
        file_resp.raise_for_status()
        outfile.parent.mkdir(parents=True, exist_ok=True)
        outfile.write_bytes(file_resp.content)
        return outfile

     

    If this helps, please consider giving Kudos. If I answered your question, mark this post as the solution.