Forum Discussion
C# XMLA TOM Connection Failing
- 9 months ago
v-sgandrathi GilbertQ I appreciate the back and forth you've provided. I have been unable to get the Tabular Object Model to work. However, I have found a way using the ADOMD connection to get the datasource names that includes the actual table or view named used in each Power BI report and this helps.
Again thank you for your time. I may revisit this as I'd like to figure out how to get TOM to work. I'm not sure if it's my .Net Framework, if it needs Core or something else.
Hi TCavins,
Thank you for following up. Based on your description, it appears the main problem is related to authentication in your connection string. Instead of using User ID and Password, try connecting with an Azure AD access token. The Tabular Object Model (TOM) supports token-based authentication, which is more secure and reliable for Power BI XMLA connections. After generating the token with MSAL (Microsoft Authentication Library), you can use it in the server.Connect() method.
Ensure your service principal has access to the Power BI workspace as an admin or with build permissions. Also, verify that your workspace is on a Premium or PPU capacity, since XMLA read/write access is only available there. Using token-based authentication should resolve the “invalid connection string” error and allow your C# code to connect.
Thank you,
Sahasra.
- TCavins10 months ago
Helper V
I believe I'm creating the token correctly. I could be wrong but it is still saying invalid connection string. I have tried multiple examples that I've found online and they all return the same. A lot of the examples use Microsoft.AnalysisServices.retail.amd64 which appears to be obsolete.
I do have a PPU license, the worksapce is set up for PPU. I'm able to authenticate and use the API to pull the report names but it doesn't give the names of the datasources.
Here's one of the examples that I've tried but still receive an invalid connection string.using System; using System.Threading.Tasks; using Microsoft.Identity.Client; // Install Microsoft.Identity.Client NuGet package using Microsoft.AnalysisServices.Tabular; // Install Microsoft.AnalysisServices.retail.amd64 NuGet package class Program { // Replace with your Azure AD app registration details private const string TenantId = "YOUR_TENANT_ID"; private const string ClientId = "YOUR_CLIENT_ID"; private const string ClientSecret = "YOUR_CLIENT_SECRET"; // For service principal auth private const string WorkspaceConnection = "powerbi://api.powerbi.com/v1.0/myorg/YOUR_WORKSPACE_NAME"; // Scope for Power BI XMLA endpoint private static readonly string[] Scopes = { "https://analysis.windows.net/powerbi/api/.default" }; static async Task Main() { try { // 1. Acquire token using MSAL (Service Principal) var app = ConfidentialClientApplicationBuilder.Create(ClientId) .WithClientSecret(ClientSecret) .WithAuthority($"https://login.microsoftonline.com/{TenantId}") .Build(); var result = await app.AcquireTokenForClient(Scopes).ExecuteAsync(); string accessToken = result.AccessToken; Console.WriteLine("✅ Token acquired successfully."); // 2. Connect to Power BI workspace via TOM using token using (var server = new Server()) { // Pass token in connection string string connectionString = $"DataSource={WorkspaceConnection};Password={accessToken};"; server.Connect(connectionString); Console.WriteLine("✅ Connected to workspace via TOM."); // Example: List all datasets (models) in the workspace foreach (var db in server.Databases) { Console.WriteLine($"Dataset: {db.Name} (ID: {db.ID})"); } server.Disconnect(); } } catch (Exception ex) { Console.WriteLine($"❌ Error: {ex.Message}"); } } }- GilbertQ10 months ago
Super User
Hi TCavins
Can you confirm that you have got the service principle set up correctly in terms of the API permissions? It needs to connect to the semantic model. Creating an Azure App or Service Principal to Call Power BI REST API | by kinsun | Medium
- TCavins10 months ago
Helper V
Here are my assigned permissions. Service principle is set as an admin on the Power BI workspace that is PPU. I appreciate the help thus far and do believe I'm overlooking/missing a small detail.