Skip to content

Instantly share code, notes, and snippets.

@data-goblin
Created April 16, 2025 16:06
Show Gist options
  • Select an option

  • Save data-goblin/cf3f22e4f73ec90ded9b139eb1a336cc to your computer and use it in GitHub Desktop.

Select an option

Save data-goblin/cf3f22e4f73ec90ded9b139eb1a336cc to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
// Custom form for collecting inputs
class InputForm : Form
{
public TextBox WorkspaceBox { get; private set; }
public TextBox ModelNameBox { get; private set; }
public TextBox ModelGuidBox { get; private set; }
public string WorkspaceName => WorkspaceBox.Text;
public string ModelName => ModelNameBox.Text;
public string ModelGuid => ExtractDatasetGuid(ModelGuidBox.Text);
public bool Confirmed { get; private set; } = false;
// Method to extract dataset GUID specifically from text or URL
private string ExtractDatasetGuid(string input)
{
if (string.IsNullOrWhiteSpace(input))
return string.Empty;
// Look for a GUID that appears after "datasets/" in the URL
Match datasetGuidMatch = Regex.Match(input, @"datasets/([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})");
if (datasetGuidMatch.Success && datasetGuidMatch.Groups.Count > 1)
return datasetGuidMatch.Groups[1].Value;
// If no match after datasets/, check if the input itself is a GUID
Match guidMatch = Regex.Match(input, "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
if (guidMatch.Success)
return guidMatch.Value;
return input; // Return original input if no GUID found
}
public InputForm()
{
Text = "Update PBIR Connection";
Width = 500;
Height = 280;
FormBorderStyle = FormBorderStyle.FixedDialog;
StartPosition = FormStartPosition.CenterScreen;
MaximizeBox = false;
MinimizeBox = false;
// Create workspace label and textbox
var workspaceLabel = new Label { Text = "Workspace Name:", Left = 20, Top = 20, Width = 120 };
WorkspaceBox = new TextBox { Left = 150, Top = 20, Width = 310 };
// Create model name label and textbox
var modelNameLabel = new Label { Text = "Model Name:", Left = 20, Top = 50, Width = 120 };
ModelNameBox = new TextBox { Left = 150, Top = 50, Width = 310 };
// Create model GUID label and textbox with description
var modelGuidLabel = new Label { Text = "Model GUID:", Left = 20, Top = 80, Width = 120 };
ModelGuidBox = new TextBox { Left = 150, Top = 80, Width = 310 };
// Add a descriptive label for the GUID field
var guidHelpLabel = new Label
{
Text = "Paste the dataset GUID or PowerBI URL (will extract GUID after 'datasets/')",
Left = 150,
Top = 105,
Width = 310,
ForeColor = System.Drawing.Color.DarkBlue,
Font = new System.Drawing.Font(Font, System.Drawing.FontStyle.Italic)
};
// Create confirm button
var confirmButton = new Button { Text = "Update", Left = 150, Top = 140, Width = 100 };
confirmButton.Click += (sender, e) => {
if (string.IsNullOrWhiteSpace(WorkspaceName) ||
string.IsNullOrWhiteSpace(ModelName))
{
MessageBox.Show("Workspace name and model name are required.");
return;
}
if (string.IsNullOrWhiteSpace(ModelGuid))
{
MessageBox.Show("Please provide a valid model GUID or URL containing the GUID.");
return;
}
// Validate that we have a proper GUID
if (!Regex.IsMatch(ModelGuid, "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"))
{
MessageBox.Show("The provided text doesn't contain a valid dataset GUID format.");
return;
}
Confirmed = true;
Close();
};
// Create cancel button
var cancelButton = new Button { Text = "Cancel", Left = 260, Top = 140, Width = 100 };
cancelButton.Click += (sender, e) => Close();
// Add controls to form
Controls.AddRange(new Control[] {
workspaceLabel, WorkspaceBox,
modelNameLabel, ModelNameBox,
modelGuidLabel, ModelGuidBox,
guidHelpLabel,
confirmButton, cancelButton
});
}
}
// Main script
void UpdatePbirFile()
{
try
{
// Show file selection dialog
var fileDialog = new OpenFileDialog
{
Title = "Select definition.pbir file",
Filter = "PBIR files (*.pbir)|*.pbir",
CheckFileExists = true
};
if (fileDialog.ShowDialog() != DialogResult.OK)
return;
string filePath = fileDialog.FileName;
// Show input dialog for workspace, model name and model GUID
var inputForm = new InputForm();
inputForm.ShowDialog();
if (!inputForm.Confirmed)
return;
string workspaceName = inputForm.WorkspaceName;
string modelName = inputForm.ModelName;
string modelGuid = inputForm.ModelGuid;
// Create the properly formatted JSON with byConnection structure
JObject jsonObj = new JObject(
new JProperty("version", "4.0"),
new JProperty("datasetReference", new JObject(
new JProperty("byConnection", new JObject(
new JProperty("connectionString", $"Data Source=powerbi://api.powerbi.com/v1.0/myorg/{workspaceName};Initial Catalog={modelName};Access Mode=readonly;Integrated Security=ClaimsToken"),
new JProperty("pbiServiceModelId", null),
new JProperty("pbiModelVirtualServerName", "sobe_wowvirtualserver"),
new JProperty("pbiModelDatabaseName", modelGuid),
new JProperty("name", "EntityDataSource"),
new JProperty("connectionType", "pbiServiceXmlaStyleLive")
))
))
);
// Save the updated PBIR file
File.WriteAllText(filePath, jsonObj.ToString(Formatting.Indented));
MessageBox.Show(
$"Successfully updated:\n- Workspace: {workspaceName}\n- Model Name: {modelName}\n- Model GUID: {modelGuid}",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// Run the script
UpdatePbirFile();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment