Using Microsoft.AnalysisServices package and Connecting to Power BI account. This the code
using Server = Microsoft.AnalysisServices.Tabular.Server;
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Calling Function");
await ConnectPowerBIAccount();
Console.ReadLine();
}
static async Task ConnectPowerBIAccount()
{
Console.WriteLine("\nPlease, connect with your Power BI account");
//Source connection for connecting to Power BI
string sourceConnection = $"powerbi://api.powerbi.com/v1.0/myorg";
string sourceConnectString = $"DataSource={sourceConnection};";
Server sourceServer = new Server();
sourceServer.Connect(sourceConnectString);
Console.WriteLine("Connected Successfully");
Thread.Sleep(3000);
}
}
After logging into Power BI account, Facing this error.
It was working earlier but when my trail license expired, Admin assigned me Premium per user and from that time, facing this issue. Able to connect with Power BI account of different tenant successfully.
I guess I'm missing some admin permission. Could anyone please help me how can I resolve this issue?
Note that: According to this MsDoc, you need to pass workspace name to connect to PowerBI.
Hence to resolve the issue, you must pass workspace name to the connection string powerbi://api.powerbi.com/v1.0/myorg/WorkSpaceName
using Server = Microsoft.AnalysisServices.Tabular.Server;
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Calling Function");
await ConnectPowerBIAccount();
Console.ReadLine();
}
static async Task ConnectPowerBIAccount()
{
Console.WriteLine("\nPlease, connect with your Power BI account");
//Source connection for connecting to Power BI
string sourceConnection = $"powerbi://api.powerbi.com/v1.0/myorg/WorkSpaceName";
string sourceConnectString = $"DataSource={sourceConnection};";
Server sourceServer = new Server();
sourceServer.Connect(sourceConnectString);
Console.WriteLine("Connected Successfully");
Thread.Sleep(3000);
}
}
As you have multiple workspaces to connect, you can pass workspace names and loop it like below:
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Calling Function");
var workspaces = new List<string> { "testruk", "testruk1" }; //Pass your workspace names
await ConnectPowerBIAccount(workspaces);
Console.ReadLine();
}
static async Task ConnectPowerBIAccount(List<string> workspaces)
{
Console.WriteLine("\nPlease, connect with your Power BI account");
string sourceConnection = $"powerbi://api.powerbi.com/v1.0/myorg";
foreach (var workspace in workspaces)
{
string sourceConnectString = $"DataSource={sourceConnection}/{workspace};";
Server sourceServer = new Server();
try
{
sourceServer.Connect(sourceConnectString);
Console.WriteLine($"Connected Successfully to {workspace}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to connect to {workspace}: {ex.Message}");
}
finally
{
sourceServer.Disconnect();
}
Thread.Sleep(1000);
}
}
}
If you want to connect to powerbi://api.powerbi.com/v1.0/myorg
, then generate access token and refer this MsDoc and blog by Aveek Das