Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Creating datasource using Power BI RestAPI

I tried creating "Excel" datasource using PowerBI Rest API. When am calling the CreateDatasource rest api call with parameters am getting error as "Bad request".

Below is the code:

private void btn_createdatasource(object sender, RoutedEventArgs e)
{
PowerBIClient pbiClient = TokenManager.GetPowerBiClient(requiredScopes);
var gateways = pbiClient.Gateways.GetGateways().Value;
var gateway = gateways.First();
var gatewaydatasources = pbiClient.Gateways.GetDatasources(gateway.Id).Value;

// string userName= ConfigurationManager.AppSettings.Get("username");
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;


var datatype = "Excel";
var name = "excelsource";
var password = ConfigurationManager.AppSettings.Get("Password");
var credentials = new WindowsCredentials(userName, password);
var credentialsEncryptor = new AsymmetricKeyEncryptor(gateway.PublicKey);
var credentialDetails = new CredentialDetails(credentials, PrivacyLevel.Private, EncryptedConnection.Encrypted, credentialsEncryptor);


var path =
@"C:\Users\sowmyas\Documents\ReportserverExcelfiles\SuperstoreSales.xlsx";
try {
var request = new PublishDatasourceToGatewayRequest(
dataSourceType: datatype,
connectionDetails: "{ \"path\":\"" + path + "\"}",
credentialDetails: credentialDetails,
dataSourceName: name);

pbiClient.Gateways.CreateDatasource(gateway.Id, request);
MessageBox.Show("Gateway Datasource successfully : " + name);
}
catch (Exception ex)
{
MessageBox.Show("Datasource not created in Gateway. Error: " + ex.Message);
}
}

 

Error Message:

 

sowmyas_1-1635855443402.png

  Name Value Type

$exception{"Operation returned an invalid status code 'BadRequest'"}Microsoft.Rest.HttpOperationException

 

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hi @Anonymous,

It seems like you send the wrong request content to the rest API so that the power bi server return the bad request error.

If you are not so sure how to configuration Datasource connections settings, you can try to publish a sample report to the power bi service side with a similar type of data source and use rest API to get the detailed configuration. After these steps, you can refer to this template to config your own data source settings.

Gateways - Get Datasource - REST API (Power BI Power BI REST APIs) | Microsoft Docs

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

1 REPLY 1
v-shex-msft
Community Support
Community Support

Hi @Anonymous,

It seems like you send the wrong request content to the rest API so that the power bi server return the bad request error.

If you are not so sure how to configuration Datasource connections settings, you can try to publish a sample report to the power bi service side with a similar type of data source and use rest API to get the detailed configuration. After these steps, you can refer to this template to config your own data source settings.

Gateways - Get Datasource - REST API (Power BI Power BI REST APIs) | Microsoft Docs

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.