Search code examples
powerbi-custom-visuals

How to store data in Powerbi for custom visual except active local storage api


I want to store user inputs to the external cloud server. But i call the api request, it will be blocked by visual's sandbox csp (Content Security Policy). I don't solve this issue. Please help me. Thanks.

setData = async (data: string): Promise<void> => {
        let url = "https://myitanalyticsdiag.blob.core.windows.net/bootdiagnostics-eusvm01-5fe205b2-e051-435a-b406-12efdd05a96a?sp=racwdli&st=2022-07-18T02:50:01Z&se=2022-07-30T10:50:01Z&spr=https&sv=2021-06-08&sr=c&sig=i0xaDxZ2jA5WA68Vh89B5tIlYJaDS4WWlgKQ%2F9kUUrs%3D"; // URL with SAS token
        if (url) {
            try {
                let resp = await fetch(url, {
                    referrer: "https://app.powerbi.com",
                    headers: {
                        "x-ms-version": "2017-11-09",
                        "x-ms-date": new Date().toISOString().split('T')[0],
                        "Content-Type": "text/plain; charset=UTF-8",
                        "x-ms-blob-content-disposition": "attachment; filename=\"mydata.txt\"",
                        "x-ms-blob-type": "BlockBlob",
                        "x-ms-meta-m1": "v1",
                        "x-ms-meta-m2": "v2",
                        "Authorization": "sp=racwdli&st=2022-07-18T02:50:01Z&se=2022-07-30T10:50:01Z&spr=https&sv=2021-06-08&sr=c&sig=i0xaDxZ2jA5WA68Vh89B5tIlYJaDS4WWlgKQ%2F9kUUrs%3D" // not needed with sas token
                    },
                    method: "PUT",
                    body: data
                });
                return Promise.resolve();
            } catch (err) {
                return Promise.reject("Request failed.");
            }
        }
        return Promise.reject("No save url defined.");
    }

Issue screenshot


Solution

  • You need to use the new Permission API available in v4.7.0: https://learn.microsoft.com/en-us/power-bi/developer/visuals/permissions-api

    Basically, you need to provide the URL capabilities.json, this way Power BI will add an exception to the visual iframe:

    "privileges": [
        {
            "name": "WebAccess",
            "essential": true,
            "parameters": [
                "https://myitanalyticsdiag.blob.core.windows.net/"
            ]
        }
    ]