Custom Scripting (Power BI, Share Point Widget)
Custom Scripting
What you will need for the script to work:
First register an app in the Azure active directory with these permissions:
SharePoint:
- Myfiles.Read [Delegated]
Power BI Service:
- Dataset.ReadWrite.All [Delegated]
- Reports.ReadWrite.All [Delegated]
Azure Service Management:
- User.impersonation [Delegated]
- Directory.AccessAsUser.All [Delegated]
- Directory.ReadWrite.All [Delegated]
- Files.ReadWrite.All [Delegated]
And please send me the following information:
- App (Client) ID
- Tenant ID
- Client Secret
- A valid and set up Redirect URI
Also please keep in mind:
According to this article, embed tokens with PRO license are intended for development and dev testing, so the number of embed tokens a Power BI master account can generate is limited. (However, the number of the limitation is not documented.) You must purchase a capacity for embedding in a production environment. There is no limit to how many embed tokens you can generate when a capacity is purchased.
Here is how to deploy the custom widget:
First thing first, here is the package that contains the node js application.
Requesting the necessary tokens:
MSFT documentation:
https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
- SharePoint
Replace the variables in the URL. And open it in a browser: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?client_id={clientId}&response_type=code&redirect_uri={redirectURI}&response_mode=query&scope=offline_access%20user.read%20mail.read%20Files.ReadWrite.All&state=12345
It will redirect you, you need to copy the "code" value from the URL. Be careful there are other values after the code one, do not copy them.
Next, you need to make a POST request (You can use postman for this) to: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
with these body parameters (type: x-www-form-urlencoded):client_id: {clientId}scope: Files.ReadWrite.Allcode: {the code from the previous step}redirect_uri: {redirectURI}grant_type: authorization_codeclient_secret: {clientSecret}
Make sure the response contains tokens. Copy the whole response and insert it to the "graph-api_auth_credentials" file in the "public" folder.
- SharePoint Site ID
You need to make a GET request (You can use postman for this) to: https://graph.microsoft.com/v1.0/sites?search={some characters of your site name to help find it}
As authorization use Bearer token, you can find this in the last requests response as "access_token".
Find your site in the response and copy it's Id (the whole value) we'll use this in a next step.
- Power BI
Replace the variables in the URL.And open this URL in a browser: https://login.microsoftonline.com/{tenant}/oauth2/authorize?client_id={clientId}&response_type=code&redirect_uri={redirectUri}&response_mode=query&resource=https://analysis.windows.net/powerbi/api&state=12345
It will redirect you, you need to copy the "code" value from the URL, be careful there are other values after the code one, do not copy them.
Next, you need to make a POST request (You can use postman for this) to: https://login.microsoftonline.com/common/oauth2/token
with these body parameters (type: x-www-form-urlencoded):client_id: {clientId}resource: https://analysis.windows.net/powerbi/apicode: {the code from the previous step}redirect_uri: {redirectURI}grant_type: authorization_codeclient_secret: {clientSecret}
Make sure the response contains tokens.Copy the whole response and insert it to the "user_auth_credentials" file in the "public" folder.
Now you need to change some constant variables inside the refreshing scriptGo to /modules/token-refresher/ctrl.token-refresher.js
And replace these variables:
const clientId = "{clientId}"; const clientSecret = "{clientSecret}"; const redirectUri = "{redirectUri}"; const tenant = "{tenantId}";
Go to /modules/image-refresher/image-refresher.js
And replace the siteId variable:
const siteId = "{sharePointSiteId}";
Repeat this step in the /modules/get-sharepoint-image/ctrl.get-sharepoint-image.js file.
At this point your application should work and refresh all the necessary tokens by itself.
How to use
Power BI widget.
https://{yourserver}/localscripts/powerBI/widget?groupId= { groupId}&reportId={reportId}
SharePoint widget: https://{yourserver}/localscripts/sharePoint/widget?path={relativePathToImage}