Example #1
0
        public async Task <CloneReportResponse[]> CloneReports(CloneReportRequest cloneReportRequest)
        {
            if (string.IsNullOrWhiteSpace(cloneReportRequest.Credential.TenantId))
            {
                throw new ValidationException(PowerResource.ValidationError_TenantIdMissing);
            }
            if (string.IsNullOrWhiteSpace(cloneReportRequest.Credential.SecretId))
            {
                throw new ValidationException(PowerResource.ValidationError_SecretIdMissing);
            }
            if (string.IsNullOrWhiteSpace(cloneReportRequest.Credential.ApplicationId))
            {
                throw new ValidationException(PowerResource.ValidationError_ApplicationIdMissing);
            }
            if (!cloneReportRequest.CloneReports.Any())
            {
                throw new ValidationException(PowerResource.ValidationError_ReportsMissingForClone);
            }
            if (string.IsNullOrWhiteSpace(cloneReportRequest.ParentWorkSpaceId))
            {
                throw new ValidationException(PowerResource.ValidationError_ParentWorkSpaceMissingForClone);
            }
            if (string.IsNullOrWhiteSpace(cloneReportRequest.ClientWorkSpaceId))
            {
                throw new ValidationException(PowerResource.ValidationError_ClientWorkSpaceMissingForClone);
            }
            if (cloneReportRequest.CloneReports.FirstOrDefault(s => string.IsNullOrWhiteSpace(s.ParentReportId)) != null)
            {
                throw new ValidationException(PowerResource.ValidationError_ParentReportsNameMissingForClone);
            }
            UserCredential = cloneReportRequest.Credential;

            if (!await AuthenticateAsync())
            {
                throw new ValidationException(PowerResource.ValidationError_AuthenticationError);
            }
            return(await Clone(cloneReportRequest));
        }
Example #2
0
        private async Task <CloneReportResponse[]> Clone(CloneReportRequest cloneReportRequest)
        {
            var responseList = new List <CloneReportResponse>();

            try
            {
                using (var pClient = new PowerBIClient(new Uri(POWER_BI_API_URL), PTokenCredentials))
                {
                    var groups = await pClient.Groups.GetGroupsWithHttpMessagesAsync();

                    var group = groups.Body.Value.FirstOrDefault(s => s.Id == cloneReportRequest.ParentWorkSpaceId);
                    if (group == null)
                    {
                        throw new ValidationException(PowerResource.ValidationErrorParentGroupNotFoundError);
                    }

                    var clientGroup = groups.Body.Value.FirstOrDefault(s => s.Id == cloneReportRequest.ClientWorkSpaceId);
                    if (clientGroup == null)
                    {
                        throw new ValidationException(PowerResource.ValidationErrorClientGroupNotFoundError);
                    }

                    var reports = await pClient.Reports.GetReportsInGroupAsync(group.Id);

                    if (reports.Value.Any())
                    {
                        foreach (var cloneReport in cloneReportRequest.CloneReports)
                        {
                            var parentReport = reports.Value.FirstOrDefault(s => s.Id == cloneReport.ParentReportId);
                            if (parentReport == null)
                            {
                                continue;
                            }

                            var export = await pClient.Reports.ExportReportInGroupAsync(@group.Id, parentReport.Id);

                            var import = await TryUploadAsync(pClient, clientGroup.Id, export, cloneReport.CloneReportName);

                            var reportDatasetId = import.Datasets.First().Id;
                            try
                            {
                                var parameter = new Dictionary <string, string> {
                                    { "ConnectionUrl", cloneReport.WebApiEndPoint }
                                };

                                var reportParameters = await pClient.Datasets.GetParametersInGroupAsync(clientGroup.Id, reportDatasetId);

                                if (reportParameters != null && reportParameters.Value.Any())
                                {
                                    await SetReportParameters(pClient, clientGroup.Id, reportDatasetId, reportParameters.Value, parameter);
                                }
                                foreach (var impDataset in import.Datasets)
                                {
                                    var refresh = await pClient.Datasets.RefreshDatasetInGroupWithHttpMessagesAsync(clientGroup.Id, impDataset.Id);
                                }
                                var clientGroupReports = await pClient.Reports.GetReportsInGroupAsync(clientGroup.Id);

                                foreach (var impReport in import.Reports)
                                {
                                    var cloneResultReport = clientGroupReports.Value.FirstOrDefault(s => s.Id == impReport.Id);
                                    await pClient.Reports.RebindReportInGroupWithHttpMessagesAsync(clientGroup.Id, impReport.Id, new RebindReportRequest { DatasetId = cloneResultReport.DatasetId });
                                }
                                responseList.Add(new CloneReportResponse
                                {
                                    CloneReportName  = cloneReport.CloneReportName,
                                    CloneReportId    = import.Reports.FirstOrDefault()?.Id,
                                    ParentReportId   = cloneReport.ParentReportId,
                                    ParentReportName = parentReport.Name,
                                    Success          = true
                                });
                            }
                            catch (Exception e) { throw e; }
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                throw new ApplicationException(exp.Message);
            }
            return(responseList.ToArray());
        }