<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic PowerBI Embedded V2 Direct Query - Azure Sql credentials not updated programatically in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Embedded-V2-Direct-Query-Azure-Sql-credentials-not/m-p/609393#M18358</link>
    <description>&lt;P&gt;I'm programatically creating a workspace and importing a Direct-Query report into PowerBI Embedded V2. Everything works fine except the report data source credentials which are not updated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code for the import flow:&lt;/P&gt;&lt;PRE&gt;await _powerBIService.ImportPbixAsync(newWorkspaceId, reportNameWithoutExtension, fileStream);
                                    Console.WriteLine("Imported report {0} for {1} ({2})", reportNameWithoutExtension, persona.Name, persona.Id);

                                    string datasetId = null;
                                    while (datasetId == null)
                                    {
                                        //get DataSource Id                
                                        Thread.Sleep(5000);
                                        datasetId = await _powerBIService.GetDatasetIdFromWorkspace(newWorkspaceId, reportNameWithoutExtension);
                                    }                                       

                                    //update the connection details
                                    await _powerBIService.UpdateConnectionAsync(newWorkspaceId, datasetId, persona.SQLUser, persona.SQLPassword);
                                    Console.WriteLine("Updated connection details for dataset {0}", reportNameWithoutExtension);

                                    // get gateway ID
                                    var gatewayId = await _powerBIService.GetGatewayIdFromWorkspaceAndDataset(newWorkspaceId, datasetId);
                                    if (gatewayId != null)
                                    {
                                        //update credentials
                                        await _powerBIService.UpdateGatewayDatasourcesCredentials(gatewayId, persona.SQLUser, persona.SQLPassword);
                                        Console.WriteLine("Updated connection details for gateway {0}", reportNameWithoutExtension);
                                    }  &lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;These are the individual methods:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;public async Task UpdateConnectionAsync(string workspaceId, string datasetId, string sqlUser, string sqlPwd)
    {
        var bearerToken = await GetBearerTokenAsync();
        var tokenCredentials = new TokenCredentials(bearerToken, "Bearer");

        using (var client = new PowerBIClient(new Uri(_pbiApiUrl), tokenCredentials))
        {
            var dataSourcesResponse = await client.Datasets.GetDatasourcesInGroupAsync(workspaceId, datasetId);
            var sqlDataSources = dataSourcesResponse.Value?.Where(s =&amp;gt; s.DatasourceType == "Sql")
                                                            .Where(s =&amp;gt; !s.ConnectionString.Contains(_reportsSQLServer) || !s.ConnectionString.Contains(_reportsSQLServer));

            foreach (var sqlDataSource in sqlDataSources)
            {
                var updateDataSourceConnectionRequest = new UpdateDatasourceConnectionRequest();
                updateDataSourceConnectionRequest.ConnectionDetails = new DatasourceConnectionDetails(_reportsSQLServer, _reportsSQLDatabase);
                //updateDataSourceConnectionRequest.DatasourceSelector = new Datasource(datasourceId: sqlDataSource.DatasourceId);

                var datasourcesRequest = new UpdateDatasourcesRequest();
                datasourcesRequest.UpdateDetails = new List&amp;lt;UpdateDatasourceConnectionRequest&amp;gt;() { updateDataSourceConnectionRequest };

                var result = await client.Datasets.UpdateDatasourcesInGroupAsync(workspaceId, datasetId, datasourcesRequest);
                //await client.Gateways.UpdateDatasourceAsync()
            }
        }
    }

public async Task UpdateGatewayDatasourcesCredentials(string gatewayId, string sqlUser, string sqlPassword)
    {
        var bearerToken = await GetBearerTokenAsync();
        var tokenCredentials = new TokenCredentials(bearerToken, "Bearer");

        using (var client = new PowerBIClient(new Uri(_pbiApiUrl), tokenCredentials))
        {
            var datasourcesResult = await client.Gateways.GetDatasourcesAsync(gatewayId);
            var sqlGatewayDatasources = datasourcesResult.Value?
                                            .Where(s =&amp;gt; s.DatasourceType == "Sql")
                                            .Where(s =&amp;gt; s.ConnectionDetails.Contains(_reportsSQLServer) &amp;amp;&amp;amp; s.ConnectionDetails.Contains(_reportsSQLDatabase));

            foreach (var gatewayDatasource in sqlGatewayDatasources)
            {
                var updateDataSourceRequest = new UpdateDatasourceRequest();
                updateDataSourceRequest.CredentialDetails = new CredentialDetails();
                updateDataSourceRequest.CredentialDetails.CredentialType = "Basic";
                updateDataSourceRequest.CredentialDetails.Credentials = "{\"credentialData\":[{\"name\":\"username\", \"value\":\"" + sqlUser + "\"},{\"name\":\"password\", \"value\":\"" + sqlPassword + "\"}]}";
                updateDataSourceRequest.CredentialDetails.EncryptedConnection = "Encrypted";
                updateDataSourceRequest.CredentialDetails.EncryptionAlgorithm = "None";
                updateDataSourceRequest.CredentialDetails.PrivacyLevel = "None";

                var result = await client.Gateways.UpdateDatasourceAsync(gatewayId, gatewayDatasource.Id, updateDataSourceRequest);
            }
        }
    }&lt;/PRE&gt;&lt;P&gt;When I attempt to view the report there is no data being fetched from the database.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions on what is missing here?&lt;/P&gt;</description>
    <pubDate>Fri, 25 Jan 2019 14:41:46 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2019-01-25T14:41:46Z</dc:date>
    <item>
      <title>PowerBI Embedded V2 Direct Query - Azure Sql credentials not updated programatically</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Embedded-V2-Direct-Query-Azure-Sql-credentials-not/m-p/609393#M18358</link>
      <description>&lt;P&gt;I'm programatically creating a workspace and importing a Direct-Query report into PowerBI Embedded V2. Everything works fine except the report data source credentials which are not updated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code for the import flow:&lt;/P&gt;&lt;PRE&gt;await _powerBIService.ImportPbixAsync(newWorkspaceId, reportNameWithoutExtension, fileStream);
                                    Console.WriteLine("Imported report {0} for {1} ({2})", reportNameWithoutExtension, persona.Name, persona.Id);

                                    string datasetId = null;
                                    while (datasetId == null)
                                    {
                                        //get DataSource Id                
                                        Thread.Sleep(5000);
                                        datasetId = await _powerBIService.GetDatasetIdFromWorkspace(newWorkspaceId, reportNameWithoutExtension);
                                    }                                       

                                    //update the connection details
                                    await _powerBIService.UpdateConnectionAsync(newWorkspaceId, datasetId, persona.SQLUser, persona.SQLPassword);
                                    Console.WriteLine("Updated connection details for dataset {0}", reportNameWithoutExtension);

                                    // get gateway ID
                                    var gatewayId = await _powerBIService.GetGatewayIdFromWorkspaceAndDataset(newWorkspaceId, datasetId);
                                    if (gatewayId != null)
                                    {
                                        //update credentials
                                        await _powerBIService.UpdateGatewayDatasourcesCredentials(gatewayId, persona.SQLUser, persona.SQLPassword);
                                        Console.WriteLine("Updated connection details for gateway {0}", reportNameWithoutExtension);
                                    }  &lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;These are the individual methods:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;public async Task UpdateConnectionAsync(string workspaceId, string datasetId, string sqlUser, string sqlPwd)
    {
        var bearerToken = await GetBearerTokenAsync();
        var tokenCredentials = new TokenCredentials(bearerToken, "Bearer");

        using (var client = new PowerBIClient(new Uri(_pbiApiUrl), tokenCredentials))
        {
            var dataSourcesResponse = await client.Datasets.GetDatasourcesInGroupAsync(workspaceId, datasetId);
            var sqlDataSources = dataSourcesResponse.Value?.Where(s =&amp;gt; s.DatasourceType == "Sql")
                                                            .Where(s =&amp;gt; !s.ConnectionString.Contains(_reportsSQLServer) || !s.ConnectionString.Contains(_reportsSQLServer));

            foreach (var sqlDataSource in sqlDataSources)
            {
                var updateDataSourceConnectionRequest = new UpdateDatasourceConnectionRequest();
                updateDataSourceConnectionRequest.ConnectionDetails = new DatasourceConnectionDetails(_reportsSQLServer, _reportsSQLDatabase);
                //updateDataSourceConnectionRequest.DatasourceSelector = new Datasource(datasourceId: sqlDataSource.DatasourceId);

                var datasourcesRequest = new UpdateDatasourcesRequest();
                datasourcesRequest.UpdateDetails = new List&amp;lt;UpdateDatasourceConnectionRequest&amp;gt;() { updateDataSourceConnectionRequest };

                var result = await client.Datasets.UpdateDatasourcesInGroupAsync(workspaceId, datasetId, datasourcesRequest);
                //await client.Gateways.UpdateDatasourceAsync()
            }
        }
    }

public async Task UpdateGatewayDatasourcesCredentials(string gatewayId, string sqlUser, string sqlPassword)
    {
        var bearerToken = await GetBearerTokenAsync();
        var tokenCredentials = new TokenCredentials(bearerToken, "Bearer");

        using (var client = new PowerBIClient(new Uri(_pbiApiUrl), tokenCredentials))
        {
            var datasourcesResult = await client.Gateways.GetDatasourcesAsync(gatewayId);
            var sqlGatewayDatasources = datasourcesResult.Value?
                                            .Where(s =&amp;gt; s.DatasourceType == "Sql")
                                            .Where(s =&amp;gt; s.ConnectionDetails.Contains(_reportsSQLServer) &amp;amp;&amp;amp; s.ConnectionDetails.Contains(_reportsSQLDatabase));

            foreach (var gatewayDatasource in sqlGatewayDatasources)
            {
                var updateDataSourceRequest = new UpdateDatasourceRequest();
                updateDataSourceRequest.CredentialDetails = new CredentialDetails();
                updateDataSourceRequest.CredentialDetails.CredentialType = "Basic";
                updateDataSourceRequest.CredentialDetails.Credentials = "{\"credentialData\":[{\"name\":\"username\", \"value\":\"" + sqlUser + "\"},{\"name\":\"password\", \"value\":\"" + sqlPassword + "\"}]}";
                updateDataSourceRequest.CredentialDetails.EncryptedConnection = "Encrypted";
                updateDataSourceRequest.CredentialDetails.EncryptionAlgorithm = "None";
                updateDataSourceRequest.CredentialDetails.PrivacyLevel = "None";

                var result = await client.Gateways.UpdateDatasourceAsync(gatewayId, gatewayDatasource.Id, updateDataSourceRequest);
            }
        }
    }&lt;/PRE&gt;&lt;P&gt;When I attempt to view the report there is no data being fetched from the database.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions on what is missing here?&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jan 2019 14:41:46 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Embedded-V2-Direct-Query-Azure-Sql-credentials-not/m-p/609393#M18358</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-01-25T14:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: PowerBI Embedded V2 Direct Query - Azure Sql credentials not updated programatically</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Embedded-V2-Direct-Query-Azure-Sql-credentials-not/m-p/639047#M18723</link>
      <description>&lt;P&gt;same problem here, seems like credentials string is incorrect but not sure how it should be reflected!&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2019 08:31:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Embedded-V2-Direct-Query-Azure-Sql-credentials-not/m-p/639047#M18723</guid>
      <dc:creator>dahund</dc:creator>
      <dc:date>2019-03-07T08:31:02Z</dc:date>
    </item>
  </channel>
</rss>

