Forum Discussion
wcwright24
6 years agoRegular Visitor
Post Import through REST API using Service Principal
Hello. I am currently trying to use the Power BI REST API to automate the process of moving PBI reports between workspaces in the Power BI Service to suit our organization's DevOps requirements. To s...
Jayendran
6 years agoSolution Sage
Hi wcwright24 ,
Using PowerBIClient SDK is the more easier method, however can you try using the API directly . Please see the code reference below
public static void ImportPBIX(string pbixFilePath, string importName) {
// delete exisitng import of the same name if on exists
DeleteImport(importName);
// create REST URL with import name in quer string
string restUrlImportPbix = ProgramConstants.PowerBiServiceRootUrl + "imports?datasetDisplayName=" + importName;
// load PBIX file into StreamContent object
var pbixBodyContent = new StreamContent(File.Open(pbixFilePath, FileMode.Open));
// add headers for request bod content
pbixBodyContent.Headers.Add("Content-Type", "application/octet-stream");
pbixBodyContent.Headers.Add("Content-Disposition",
@"form-data; name=""file""; filename=""" + pbixFilePath + @"""");
// load PBIX content into body using multi-part form data
MultipartFormDataContent requestBody = new MultipartFormDataContent(Guid.NewGuid().ToString());
requestBody.Add(pbixBodyContent);
// create and configure HttpClient
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AccessToken);
// post request
var response = client.PostAsync(restUrlImportPbix, requestBody).Result;
// check for success
if (response.StatusCode.ToString().Equals("Accepted")) {
Console.WriteLine("Import process complete: " + response.Content.ReadAsStringAsync().Result);
}
You can find the full source code from the below