Follow these steps and examples to migrate your application smoothly from version v4.22 to v5.
-
Authentication Updates
Replace the previously usedMicrosoft.Rest.ClientRuntime.Propertiescredential package with theAzure.Identitypackage for authentication.Username Password Authentication:
var credentials = new UsernamePasswordCredential("username", "password", "tenantID", "ClientId"); PowerBIClient client = new PowerBIClient(credentials);
For further refernce, refer to the UsernamePasswordCredential
Service Prinicipal Authentication:
var credentials = new ClientSecretCredential("tenantId", "clientId", "clientSecret"); PowerBIClient client = new PowerBIClient(credentials);
For further reference, refer to the ClientSecretCredential
-
Read-Only Property Access
In versionv5, some properties that could previously be modified now been marked as read-only. As a result, direct assignment to these properties will throw an error similar to: Property or indexer '' cannot be assigned to -- it is read only.Use the
Addmethod.Refer to the code example with v5 package for intializing the required workspaces:
using (PowerBIClient client = new PowerBIClient(credentials or Microsoft Entra Access Token)) { var workspaces = new RequiredWorkspaces(); workspaces.Workspaces.Add(groupId); var workspaceInfo = client.WorkspaceInfo.PostWorkspaceInfo(workspaces); }
-
Gateway - Create Datasource:
Creates a new data source on the specified on-premises gateway. It supports only on-premises gateways, and the user must have gateway admin permissions.
In versionv5, theGatewayPublicKeyclass now has internal constructors. As a result, you can no longer directly instantiate theGatewayPublicKeyclass using its constructor. Instead, you must use the factory method provided by the SDK.Refer to the code example with v5 package for creating a new data source:
using (PowerBIClient client = new PowerBIClient(credentials or Microsoft Entra Access Token)) { var basicCredentials = new BasicCredentials(username: "username", password: "password"); var publicKey = MicrosoftPowerBIApiModelFactory.GatewayPublicKey( exponent: "Exponent of the gateway public key", modulus: "Modulus of the gateway public key" ); var credentialsEncryptor = new AsymmetricKeyEncryptor(publicKey); var credentialDetails = new CredentialDetails( basicCredentials, CredentialType.Basic, PrivacyLevel.None, EncryptedConnection.Encrypted, credentialsEncryptor ); var pdtgateway = new PublishDatasourceToGatewayRequest( dataSourceType: "SQL", connectionDetails: JsonConvert.SerializeObject(new { server = "Server name", database = "Database name" }), credentialDetails: credentialDetails, dataSourceName: "SampleDataSource" ); var newDataSource = client.Gateways.CreateDatasource(gatewayId, pdtgateway); }