Skip to content

Commit 72b61d3

Browse files
committed
Merged PR 5212: Merged PR 4479: Get Group Id from config or url
Merged PR 4479: Get Group Id from config or url Cherry-picked from commit `1c948f66`.
1 parent 64d509f commit 72b61d3

File tree

3 files changed

+91
-6
lines changed

3 files changed

+91
-6
lines changed

src/create.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export class Create extends embed.Embed {
5151
datasetId: config.datasetId || this.getId(),
5252
accessToken: config.accessToken,
5353
tokenType: config.tokenType,
54-
settings: settings
54+
settings: settings,
55+
groupId: config.groupId
5556
}
5657
}
5758

src/embed.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface IEmbedConfigurationBase {
3434
type?: string;
3535
accessToken?: string;
3636
tokenType?: models.TokenType;
37+
groupId?: string;
3738
}
3839

3940
// TODO: Re-use ILoadConfiguration interface to prevent duplicating properties.
@@ -418,6 +419,7 @@ export abstract class Embed {
418419
this.config.uniqueId = this.getUniqueId();
419420
this.config.embedUrl = this.getEmbedUrl();
420421
this.config.accessToken = this.getAccessToken(this.service.accessToken);
422+
this.config.groupId = this.getGroupId();
421423
this.addLocaleToEmbedUrl(config);
422424
}
423425

@@ -467,6 +469,16 @@ export abstract class Embed {
467469
return this.config.uniqueId || this.element.getAttribute(Embed.nameAttribute) || utils.createRandomString();
468470
}
469471

472+
/**
473+
* Gets the group ID from the first available location: options, embeddedUrl.
474+
*
475+
* @private
476+
* @returns {string}
477+
*/
478+
private getGroupId(): string {
479+
return this.config.groupId || Embed.findGroupIdFromEmbedUrl(this.config.embedUrl);
480+
}
481+
470482
/**
471483
* Gets the report ID from the first available location: options, attribute.
472484
*
@@ -539,4 +551,24 @@ export abstract class Embed {
539551
this.iframe.addEventListener('load', () => this.createReport(this.createConfig), false);
540552
}
541553
}
554+
555+
/**
556+
* Adds the ability to get groupId from url.
557+
* By extracting the ID we can ensure that the ID is always explicitly provided as part of the load configuration.
558+
*
559+
* @static
560+
* @param {string} url
561+
* @returns {string}
562+
*/
563+
static findGroupIdFromEmbedUrl(url: string): string {
564+
const groupIdRegEx = /groupId="?([^&]+)"?/
565+
const groupIdMatch = url.match(groupIdRegEx);
566+
567+
let groupId;
568+
if (groupIdMatch) {
569+
groupId = groupIdMatch[1];
570+
}
571+
572+
return groupId;
573+
}
542574
}

test/test.spec.ts

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function ValidateDashboardConfigurationWorksAsExpected(pageView: string, excepti
3131
const dashboardEmbedConfig = {
3232
type: "dashboard",
3333
id: "fakeReportId",
34+
groupId: "fakeGroupId",
3435
accessToken: "fakeAccessToken",
3536
embedUrl: "fakeEmbedUrl",
3637
pageView: pageView
@@ -179,14 +180,16 @@ describe('service', function () {
179180
type: "report",
180181
id: "fakeReportId",
181182
accessToken: "fakeAccessToken",
182-
embedUrl: "fakeEmbedUrl"
183+
embedUrl: "fakeEmbedUrl",
184+
groupId: "fakeGroupId",
183185
};
184186

185187
const dashboardEmbedConfig: embed.IEmbedConfiguration = {
186188
type: "dashboard",
187189
id: "fakeDashboardId",
188190
accessToken: "fakeAccessToken",
189-
embedUrl: "fakeEmbedUrl"
191+
embedUrl: "fakeEmbedUrl",
192+
groupId: "fakeGroupId"
190193
};
191194

192195
powerbi.embed(component[0], reportEmbedConfig);
@@ -209,13 +212,15 @@ describe('service', function () {
209212
accessToken: "fakeAccessToken",
210213
embedUrl: 'fakeUrl',
211214
id: 'report2',
212-
type: 'report'
215+
type: 'report',
216+
groupId: "fakeGroupId"
213217
};
214218

215219
const createConfig: embed.IEmbedConfiguration = {
216220
datasetId: "fakeDashboardId",
217221
accessToken: "fakeAccessToken",
218-
embedUrl: "fakeEmbedUrl"
222+
embedUrl: "fakeEmbedUrl",
223+
groupId: "fakeGroupId"
219224
};
220225

221226
// Act
@@ -245,7 +250,8 @@ describe('service', function () {
245250
language: 'languageName',
246251
formatLocale: 'formatName'
247252
}
248-
}
253+
},
254+
groupId: "fakeGroupId"
249255
};
250256

251257
powerbi.embed($reportContainer[0], testConfiguration);
@@ -366,6 +372,52 @@ describe('service', function () {
366372
expect(report.config.uniqueId).toEqual(jasmine.any(String));
367373
});
368374

375+
it('should get group id from configuration first', function () {
376+
// Arrange
377+
const testGroupId = "ABC123";
378+
const embedUrl = `https://embedded.powerbi.com/appTokenReportEmbed?groupId=DIFFERENTID`;
379+
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}" powerbi-type="report"></div>`)
380+
.appendTo('#powerbi-fixture');
381+
382+
const configuration: embed.IEmbedConfiguration = { id: 'fakeId' , groupId: testGroupId };
383+
384+
// Act
385+
const report = powerbi.embed($reportContainer[0], configuration);
386+
387+
// Assert
388+
expect((<embed.IEmbedConfiguration>report.config).groupId).toEqual(testGroupId);
389+
});
390+
391+
it('should get groupId from embeddUrl is not specified in config', function () {
392+
// Arrange
393+
const embedUrl = `https://embedded.powerbi.com/appTokenReportEmbed?groupId=DIFFERENTID`;
394+
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}" powerbi-type="report"></div>`)
395+
.appendTo('#powerbi-fixture');
396+
397+
const configuration: embed.IEmbedConfiguration = {id: 'fakeId'};
398+
399+
// Act
400+
const report = powerbi.embed($reportContainer[0], configuration);
401+
402+
// Assert
403+
expect((<embed.IEmbedConfiguration>report.config).groupId).toEqual('DIFFERENTID');
404+
});
405+
406+
it('should get groupId undefined if not specified in embeddUrl or config', function () {
407+
// Arrange
408+
const embedUrl = `https://embedded.powerbi.com/appTokenReportEmbed?reportId=fakeId`;
409+
const $reportContainer = $(`<div powerbi-embed-url="${embedUrl}" powerbi-type="report"></div>`)
410+
.appendTo('#powerbi-fixture');
411+
412+
const configuration: embed.IEmbedConfiguration = {id: 'fakeId'};
413+
414+
// Act
415+
const report = powerbi.embed($reportContainer[0], configuration);
416+
417+
// Assert
418+
expect((<embed.IEmbedConfiguration>report.config).groupId).toBeUndefined();
419+
});
420+
369421
it('should get filterPaneEnabled setting from attribute from config and then attribute', function () {
370422
// Arrange
371423
const testUniqueId = 'fakeUniqueId';

0 commit comments

Comments
 (0)