Forum Discussion
Datasets.Extensions.PostRowsAsync() - param documentation?
- 9 years ago
See a demo which works in my test.
By the way, there's no replacement for the SDK, as the collectionname and workspaceid are the concepts in the being deprecated Power BI Embedded. The new "embedding-with-non-power-bi-users" is embeding reports from Power BI Service instead of Azure PBI workspace.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.PowerBI.Api.V1; using Microsoft.Rest; using Microsoft.PowerBI.Api.V1.Models; namespace ConsoleApplication39 { class Program { static string accesskey = "KJixsmmw+Ncxxxxx23xxxxxxcaOIFr6a2vmQ=="; static string workspaceCollectionName = "cisxxxxexo"; static string workspaceId = "79c71931-dexxxxa3cxxxd2192fe0b"; static void Main(string[] args) { var credentials = new TokenCredentials(accesskey, "AppKey"); // Instantiate your Power BI client passing in the required credentials var client = new PowerBIClient(credentials); // Override the api endpoint base URL. Default value is https://api.powerbi.com client.BaseUri = new Uri("https://api.powerbi.com"); //create a dataset and get datasetkey string datasetID = CreateDatasets(workspaceCollectionName, workspaceId,client); string data = @"{ ""rows"": [ { ""id"": 1, ""name"": ""Tom""}, { ""id"": 2, ""name"": ""Jerry""} ] }"; object dataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Object>(data.ToString()); PostRows(workspaceCollectionName,workspaceId,datasetID,"testtable", dataObj, client); } static void PostRows(string workspaceCollectionName, string workspaceId, string datasetid, string tableName, object datajson, PowerBIClient client ) { //public static object PostRows(this IDatasets operations, string collectionName, string workspaceId, string datasetKey, string tableName, object requestMessage); var response = client.Datasets.PostRows(workspaceCollectionName, workspaceId, datasetid, tableName,datajson); } static string CreateDatasets(string workspaceCollectionName, string workspaceId, PowerBIClient client) { Dataset ds = new Dataset(); ds.Name = "testdataset"; Table table1 = new Table(); table1.Name = "testTable"; Column column1 = new Column("id","Int64"); Column column2 = new Column("name", "string"); table1.Columns = new List<Column>() { column1, column2 }; ds.Tables = new List<Table>() { table1}; var response = client.Datasets.PostDataset(workspaceCollectionName, workspaceId, ds); dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Object>(response.ToString()); return obj["id"].ToString(); } } }
Hmmm...I suspect that, technically speaking this is exactly the answer to the question that I asked - but ultimately I'm running into something else that I think will force me to rethink my approach. I'm running into this "Data [dataset id] is not Push API dataset" error that others have reported.
My understanding is that you can't add rows to a dataset that exists in the service when this dataset originates from a .pbix file that was uploaded. If you create a dataset with the REST API, adding rows works, but for a dataset coming from a .pbix, it'll result in this "...not Push API dataset" error.
I used the desktop version of Power BI to create my dataset, and some reports. That got saved in a local .pbix file. I then need to upload the whole thing, and update the data on a regular basis. While I could create the dataset from scratch through the REST API, it's not clear to me how I could get the locally-created reports uploaded on their own, if they have to be separated from the dataset--and then the next problem would be to somehow link the reports with the dataset.
I've looked at getting my local .pbix file refreshed programmatically (and then re-uploading the whole thing, after deleting anything that might already have been uploaded previously), but it looks like there's no API to do this.
What's the approach people take to do this sort of thing? Seems to me the end goal is pretty simple: A local .pbix defines reports, I want them uploaded to the service, and I want to programmatically pump new data into the dataset on a regular basis.
Hi Dandy72,
While reading the comments I am exactly in same situation like you explained below. Were you able to solve the problem ? Please share your idea if you have implemented according to your workflow?
- Anonymous7 years agoNot applicable
This works, assuming you have the ID of the PowerBI dataset you're updating, the table name in the dataset, and your auth token:
public async Task<object> AddRowsToTableAsync(string datasetId, string tableName, object rows, string token) { var tokenCredentials = new TokenCredentials(token, "Bearer"); var ret = new object(); using (var c = new PowerBIClient(new Uri(PowerBIDatasetsApiUrl), tokenCredentials)) { ret = await c.Datasets.PostRowsAsync(datasetId, tableName, rows); } return ret; }Where for example 'rows' is a List<SerializableMyDataRow>, and SerializableMyDataRow is:
public class SerializableMyDataRow { [JsonProperty("Account")] public string Account { get; set; } [JsonProperty("Reference")] public string Reference { get; set; } [JsonProperty("Value")] public decimal Value { get; set; } }The properties there match the column names and types in the PowerBI table.