<?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 Re: Power BI REST API Update datasource credentials in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1204430#M24601</link>
    <description>&lt;P&gt;Are you looking for Power BI .NET SDK v3 code to update datasource credentials. Here is a code snippet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;public static void PatchSqlDatasourceCredentials(Guid WorkspaceId, string DatasetId, string UserName, string UserPassword) {

      PowerBIClient pbiClient = TokenManager.GetPowerBiClient(requiredScopes);
      var datasources = (pbiClient.Datasets.GetDatasourcesInGroup(WorkspaceId, DatasetId)).Value;
      // find the target SQL datasource
      foreach (var datasource in datasources) {
        if (datasource.DatasourceType.ToLower() == "sql") {
          // get the datasourceId and the gatewayId
          var datasourceId = datasource.DatasourceId;
          var gatewayId = datasource.GatewayId;
          // Create UpdateDatasourceRequest to update Azure SQL datasource credentials
          UpdateDatasourceRequest req = new UpdateDatasourceRequest {
            CredentialDetails = new CredentialDetails(
              new BasicCredentials(UserName, UserPassword),
              PrivacyLevel.None,
              EncryptedConnection.NotEncrypted)
          };
          // Execute Patch command to update Azure SQL datasource credentials
          pbiClient.Gateways.UpdateDatasource((Guid)gatewayId, (Guid)datasourceId, req);
        }
      };

    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Source code can be found here:&amp;nbsp;&lt;A href="https://github.com/TedPattison/DatasetManagement/blob/master/DatasetManagement/Models/PowerBiManager.cs" target="_blank"&gt;https://github.com/TedPattison/DatasetManagement/blob/master/DatasetManagement/Models/PowerBiManager.cs&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 06 Jul 2020 17:23:28 GMT</pubDate>
    <dc:creator>TedPattison</dc:creator>
    <dc:date>2020-07-06T17:23:28Z</dc:date>
    <item>
      <title>Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1198628#M24541</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible to use the Powerbi REST API to update the login credentials to a dataset?&lt;/P&gt;&lt;P&gt;I found something similar for gateway-managed dataset:&amp;nbsp;&lt;A href="https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/adddatasourceuser#example" target="_blank" rel="noopener"&gt;https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/adddatasourceuser#example&lt;/A&gt;&amp;nbsp;but all my dataset are on Azure Cloud (Azure SQL) so I can't use gateway.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 17:43:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1198628#M24541</guid>
      <dc:creator>ivanferr92</dc:creator>
      <dc:date>2020-07-02T17:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1202780#M24594</link>
      <description>&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;HI&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/243248"&gt;@ivanferr92&lt;/a&gt;,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;I also check the rest API documents but not found any apis to direct operate the data source credentials. (some of API can update the connection strings but they not mention/contains any parameters to operate on data credentials)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;I guess power bi service may not want to share them due to security reasons.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Regards,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Xiaoxin Sheng&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 06:32:16 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1202780#M24594</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-07-06T06:32:16Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1204430#M24601</link>
      <description>&lt;P&gt;Are you looking for Power BI .NET SDK v3 code to update datasource credentials. Here is a code snippet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;public static void PatchSqlDatasourceCredentials(Guid WorkspaceId, string DatasetId, string UserName, string UserPassword) {

      PowerBIClient pbiClient = TokenManager.GetPowerBiClient(requiredScopes);
      var datasources = (pbiClient.Datasets.GetDatasourcesInGroup(WorkspaceId, DatasetId)).Value;
      // find the target SQL datasource
      foreach (var datasource in datasources) {
        if (datasource.DatasourceType.ToLower() == "sql") {
          // get the datasourceId and the gatewayId
          var datasourceId = datasource.DatasourceId;
          var gatewayId = datasource.GatewayId;
          // Create UpdateDatasourceRequest to update Azure SQL datasource credentials
          UpdateDatasourceRequest req = new UpdateDatasourceRequest {
            CredentialDetails = new CredentialDetails(
              new BasicCredentials(UserName, UserPassword),
              PrivacyLevel.None,
              EncryptedConnection.NotEncrypted)
          };
          // Execute Patch command to update Azure SQL datasource credentials
          pbiClient.Gateways.UpdateDatasource((Guid)gatewayId, (Guid)datasourceId, req);
        }
      };

    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Source code can be found here:&amp;nbsp;&lt;A href="https://github.com/TedPattison/DatasetManagement/blob/master/DatasetManagement/Models/PowerBiManager.cs" target="_blank"&gt;https://github.com/TedPattison/DatasetManagement/blob/master/DatasetManagement/Models/PowerBiManager.cs&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 17:23:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1204430#M24601</guid>
      <dc:creator>TedPattison</dc:creator>
      <dc:date>2020-07-06T17:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1660488#M27792</link>
      <description>&lt;P&gt;above code throw "Operation returned an invalid status code 'Unauthorized'&amp;nbsp; on "client.Gateways.UpdateDatasource"&amp;nbsp; &amp;nbsp;event thou I have given all powerBI API permission include Tenant.ReadWrite.&amp;nbsp; &amp;nbsp;I don't use the gateway, Is this the reason for the error?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2021 12:12:04 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1660488#M27792</guid>
      <dc:creator>paritoshmishra</dc:creator>
      <dc:date>2021-02-11T12:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1660502#M27794</link>
      <description>&lt;P&gt;There is still a gateway ID and you need to use the Gateway API even when there is no actual Gateway.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are you using service principal? If so, did you go through all the proper configuration to enable service principal access to Power BI API in your tenant and add servide principal as gatewway admin?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2021 12:23:07 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1660502#M27794</guid>
      <dc:creator>TedPattison</dc:creator>
      <dc:date>2021-02-11T12:23:07Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1818363#M29258</link>
      <description>&lt;P&gt;It's still not clear to me how this works as for Azure datasources, the docs say that the credentials are "per user" and so even if setting the credentals by an SP, wouldn't each user then have to enter the credentials themselves anyway?&lt;/P&gt;</description>
      <pubDate>Sun, 02 May 2021 09:32:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/1818363#M29258</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-05-02T09:32:40Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/2357225#M34598</link>
      <description>&lt;P&gt;When there is no actual gateway, and it's a cloud datasource, I'm able to find a gateway ID as you say, interestingly enough.&lt;BR /&gt;&lt;BR /&gt;However, when I try to update the datasource using this gateway ID + datasource ID, I always get an error: 400&amp;nbsp;&lt;SPAN&gt;DM_GWPipeline_Gateway_DataSourceAccessError. I also can't lookup this gateway using the get gateway method and retrieve its public key, which I need in order to encrypt the credentials.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Feb 2022 22:10:10 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/2357225#M34598</guid>
      <dc:creator>xli629b</dc:creator>
      <dc:date>2022-02-23T22:10:10Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/2953018#M40165</link>
      <description>&lt;P&gt;Hi, Are you able to find the solution of this problem?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 05:14:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/2953018#M40165</guid>
      <dc:creator>nishanttayal</dc:creator>
      <dc:date>2022-12-07T05:14:27Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/3052993#M40981</link>
      <description>&lt;P&gt;Has anyone been able to solve this datasource update for a cloud (not on-prem) gateway?&lt;BR /&gt;In my case I need to update a GCP Service Account on a Google Big Quey datasource &lt;STRONG&gt;on a dataflow&lt;/STRONG&gt;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A&lt;BR /&gt;Using the Gateways - Update Datasource "REST API Try It":&lt;BR /&gt;If I use the service principal I get a "PowerBINotAuthorizedException",&lt;/P&gt;&lt;P&gt;If I use my own creds, I get an error:&amp;nbsp;DM_GWPipeline_Gateway_DataSourceAccessError.&lt;/P&gt;&lt;P&gt;I have not found an explanation for this in the cloud datasource context.&amp;nbsp;&amp;nbsp;Does this API work at all for personal, cloud gateway datasources? It seems most of the gateway API's were designed around on-prem gateways.&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;B&lt;/DIV&gt;&lt;DIV&gt;Also, trying the same using Powershell, I'm getting a BadRequest (400). H&lt;SPAN&gt;ow to compile the basic credentials in the request body in PowerShell where the GCP service account key is an entire JSON file full of double quotes which need to be escaped or all enclosed in single quotes?&lt;BR /&gt;&lt;BR /&gt;Any help would be most welcome please.&lt;BR /&gt;Also, I'm not sure if I should post this in a new topic?&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 04 Feb 2023 00:31:12 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/3052993#M40981</guid>
      <dc:creator>Andrew-T2</dc:creator>
      <dc:date>2023-02-04T00:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/4633154#M60139</link>
      <description>&lt;P&gt;The above code throws me a BadRequest.&lt;BR /&gt;Has someone this working, so set the DataSource per model&amp;nbsp; (and not set the shared one)&lt;/P&gt;</description>
      <pubDate>Tue, 01 Apr 2025 11:54:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/4633154#M60139</guid>
      <dc:creator>BartHuls</dc:creator>
      <dc:date>2025-04-01T11:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI REST API Update datasource credentials</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/4633886#M60148</link>
      <description>&lt;P&gt;btw I came up with the following implementation:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;  public async Task PatchSqlDatasourceCredentials(Guid groupId, string datasetId, string reportingSqlServer, string reportingSqldatabase, string userName, string password)
  {
      var pbiClient = await GetPowerBIClient() ?? throw new PowerBiException(PowerBiClientIsNull);

      var datasources = (await pbiClient.Datasets.GetDatasourcesInGroupAsync(groupId, datasetId)).Value;

      var sqlDataSource = datasources.FirstOrDefault(d =&amp;gt; d.DatasourceType.Equals("Sql", StringComparison.OrdinalIgnoreCase)) ?? throw new PowerBiException(PowerBiClientIsNull);

      var selectedServer = sqlDataSource.ConnectionDetails.Server;
      var selectedDatabase = sqlDataSource.ConnectionDetails.Database;

      if (!selectedServer.Equals(reportingSqlServer, StringComparison.OrdinalIgnoreCase) ||
          !selectedDatabase.Equals(reportingSqldatabase, StringComparison.OrdinalIgnoreCase))
      {
          await pbiClient.Datasets.UpdateDatasourcesInGroupAsync(groupId, datasetId, new UpdateDatasourcesRequest(new UpdateDatasourceConnectionRequest
          {
              DatasourceSelector = new Datasource
              {
                  DatasourceType = "Sql",
                  ConnectionDetails = new DatasourceConnectionDetails()
                  {
                      Server = selectedServer,
                      Database = selectedDatabase
                  }
              },
              ConnectionDetails = new DatasourceConnectionDetails()
              {
                  Server = reportingSqlServer,
                  Database = reportingSqldatabase
              },

          }));
      }

      datasources = (await pbiClient.Datasets.GetDatasourcesInGroupAsync(groupId, datasetId)).Value;
      sqlDataSource = datasources.FirstOrDefault(d =&amp;gt; d.DatasourceType.Equals("Sql", StringComparison.OrdinalIgnoreCase)) ?? throw new PowerBiException(PowerBiClientIsNull);

      var datasourceId = sqlDataSource.DatasourceId!.Value;
      var gatewayId = sqlDataSource.GatewayId!.Value;

      // Create UpdateDatasourceRequest to update Azure SQL datasource credentials
      var updateDatasourceRequest = new UpdateDatasourceRequest
      {
          CredentialDetails = new CredentialDetails(
              new BasicCredentials(userName, password),
              PrivacyLevel.None,
              EncryptedConnection.NotEncrypted)
      };

      // Execute Patch command to update Azure SQL datasource credentials
      await pbiClient.Gateways.UpdateDatasourceAsync(gatewayId, datasourceId, updateDatasourceRequest);
  }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Apr 2025 19:44:44 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Power-BI-REST-API-Update-datasource-credentials/m-p/4633886#M60148</guid>
      <dc:creator>BartHuls</dc:creator>
      <dc:date>2025-04-01T19:44:44Z</dc:date>
    </item>
  </channel>
</rss>

