Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
9 years ago
Solved

Unable to update dataset credentials after uploading a pbix report

So I have been working on being able to import pbix reports through my application and then being able to view them through it.  I have this almost all teh way working right now.  The only issue stil...
  • Anonymous's avatar
    Anonymous
    9 years ago

    So for anyone that is struggling with this, or anything else that goes along with trying to work with the PowerBI API when trying to use the new PowerBI Premium stuff, I actually found this video, and project that goes along with it, that has EVERYTHING you need.  The video expains it step by step, and should answer any questions you have, like I did, on what things mean, or how to proceed in certian situations, ect...

     

    The one thing his project does not do, that I had to do to make the Patch credentials, and get DataSets work, is to add in the groups/{GroupID}/ to the URL directly after myorg/.  The reason for this is in his project he is just using MyWorkspaces in PowerBI.com.  However, I needed to use a an App workspace that was created for just my project.  If you just use the URL he shows, without adding the groups stuff in, you will only see datasets in MyWorkspace.   You can get the group ID from the URL when you are in PowerBI.com, and click on your app workspace.  Just FYI.

     

    The URL for Patching the credentials in PowerBI ends up looking like...

    https://api.powerbi.com/v1.0/myorg/groups/{GroupId}/........

     

    Below here is the link to the video.

     

    http://curvetube.com/Publishing_PBIX_Files_with_the_Power_BI_REST_API/ZSaBFf3ziUk.video

     

    Below this is the link to the project that goes along with the above video...

     

    https://github.com/CriticalPathTraining/PbixInstallerForPowerBI

     

    Hope this all helps you as much as it did me.  Below is the function I used to Patch the dataset also.

     

    Stizz001

     

    /// <summary>
            /// Resets the credentials of a dataset after the PBIX was imported. After import, a direct query dataset loses its credentials
            /// </summary>
            /// <param name="importName">Name of the imported file</param>
            /// <param name="sqlAzureUser">The user name for the datasource</param>
            /// <param name="sqlAzurePassword">the users password for the datasource</param>
            private static void PatchDatasourceCredentials(string importName, string sqlAzureUser, string sqlAzurePassword)
            {
    
                string restUrlDatasets = PowerBiServiceRootUrl + "/groups/" + GroupId + "/datasets/";
                string jsonDatasets = ExecuteGetRequest(restUrlDatasets);
    
                DatasetCollection datasets = JsonConvert.DeserializeObject<DatasetCollection>(jsonDatasets);
                foreach (var dataset in datasets.value)
                {
                    // find dataset whose name matches import name
                    if (importName.Equals(dataset.name))
                    {
                        //Console.WriteLine("Updating data source for dataset named " + dataset.name);
                        // determine gateway id and datasoure id
                        string restUrlDatasetToUpdate = restUrlDatasets + dataset.id + "/";
                        string restUrlDatasetDefaultGateway = restUrlDatasetToUpdate + "Default.GetBoundGatewayDataSources";
                        string jsonDefaultGateway = ExecuteGetRequest(restUrlDatasetDefaultGateway);
    
                        PbixInstallerForPowerBI.Model.Gateway defaultGateway = (JsonConvert.DeserializeObject<GatewayCollection>(jsonDefaultGateway)).value[0];
                       
                        // create URL with pattern myorg/gateways/{gateway_id}/datasources/{datasource_id}
                        string restUrlPatchCredentials =
                          PowerBiServiceRootUrl +
                          "gateways/" + defaultGateway.gatewayId + "/" +
                          "datasources/" + defaultGateway.id + "/";
    
                        // create C# object with credential data
                        DataSourceCredentials dataSourceCredentials =
                          new DataSourceCredentials
                          {
                              credentialType = "Basic",
                              basicCredentials = new PbixInstallerForPowerBI.Model.BasicCredentials
                              {
                                  username = sqlAzureUser,
                                  password = sqlAzurePassword
                              }
                          };
    
                        // serialize C# object into JSON
                        string jsonDelta = JsonConvert.SerializeObject(dataSourceCredentials);
    
                        // add JSON to HttpContent object and configure content type
                        HttpContent patchRequestBody = new StringContent(jsonDelta);
                        patchRequestBody.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
    
                        // prepare PATCH request
                        var method = new HttpMethod("PATCH");
                        var request = new HttpRequestMessage(method, restUrlPatchCredentials);
                        request.Content = patchRequestBody;
    
                        HttpClient client = new HttpClient();
                        client.DefaultRequestHeaders.Add("Accept", "application/json");
                        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
    
                        client.SendAsync(request);
                    }
                }
            }

    where

    PowerBiServiceRootUrl  = "https://api.powerbi.com/v1.0/myorg";