New REST API of Power BI is revolutionary. In addition to embedding content in Power BI, ability to refresh data set from API, it also gives you many functions to work with Gateways and Data Sources. With this API, you can set up new data sources, clone data sets, check the credentials of a data source, get list of all data sources under a gateway, and do many other operations. In other words; Data source management can be fully automated with REST API. Continue reading this post if you like to learn more about this. If you like to learn more about Power BI; read Power BI book; from Rookie to Rock Star.
Step 1: Register your Application
Step 2: Authenticate
The sample code for this example can be downloaded from here.
Get List of Gateways
List of gateways are accessible through a function under Gateways, called GetGatewaysAsync. you can easly fetch list of gateways (on-premises gateways, not personal), and then loop through these.
var gateways = await client.Gateways.GetGatewaysAsync();
This function as you can see in above code screenshot will give you list of all on-premises data gateways defined under your account. then you can loop through each gateway and get the information of that gateway, for example; name of the gateway, Id of it, and some information such as version.
Here are list of gateways under this Power BI account;
As you can see all of above gateways can be accessed through REST API.
Get List of Data Sources
Under a gateway, you can access all data sources with a GetDataSourcesAsync function.
var dashboards = await client.Dashboards.GetDashboardsInGroupAsync(GroupId);
which match exactly the configuration of that data source in the Power BI service
As you can see; there are also properties for accessing the credentials. or even changing it.
Data Source Management
In addition to reading list of data sources, you can do some other operations such as creating a data source, setting up credentials for it. and even binding a data source to a gateway.
Create Data Source
There is a function to create data source, called CreateDatasourceAsync. You need to specify data source through a new object of PublishDatasourceToGatewayRequest for that. this object can contain type of data set, and connection information about it including credentials.
Binding Data Set to a Gateway
after creating the data source, then you can bind a data set to that gateway. For finding the particular data set, please read the previous blog post. when you have the data set key, then you can easily use BindToGatewayInGroupAsync function;
client.Datasets.BindToGatewayInGroupAsync(GroupId, datasetKey, new BindToGatewayRequest(gateway.Id));
There are many other data source and gateway functions that you can use, make sure you explore the REST API and find out more about all functions. Next post will be about posting data rows to a real-time streaming data set through REST API. Stay tuned.