csharp power BI博客的示例代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp power BI博客的示例代码相关的知识,希望对你有一定的参考价值。

Innovator innovator = this.getInnovator(); 
    
    string item = this.getProperty("item", "");
    string name = this.getProperty("itemName", "");
    
    Item result = innovator.applyMethod("PowerBI_GetToken", "");
    string accessToken = result.getResult();
	
    Item results = innovator.newItem();
    
    if (accessToken != "") {
        if (item == "report") {
            
            string ReturnXML = "";
            Report[] reports = GetReports(accessToken);
            Report report = (from r in reports where r.name == name select r).FirstOrDefault();
           
            ReturnXML += "<Item type='Report'>";
            ReturnXML += "<id>" + report.id + "</id>";
            ReturnXML += "<name>" + report.name + "</name>";
            ReturnXML += "<webUrl><![CDATA[" + report.webUrl + "]]></webUrl>";
            ReturnXML += "<embedUrl><![CDATA[" + report.embedUrl + "]]></embedUrl>";
            ReturnXML += "<token>" + accessToken + "</token>";
            ReturnXML += "</Item>";   
    
            ReturnXML = "<Envelope><Body><Result>" + ReturnXML + "</Result></Body></Envelope>";
            results.dom.LoadXml(ReturnXML);
        }
        else if (item == "dashboard") {
            
            string ReturnXML = "";
            Dashboard[] dashboards = GetDashboards(accessToken);
            Dashboard dashboard = (from d in dashboards where d.displayName == name select d).FirstOrDefault();
            
            ReturnXML += "<Item type='Dashboard'>";
            ReturnXML += "<id>" + dashboard.id + "</id>";
            ReturnXML += "<displayName>" + dashboard.displayName + "</displayName>";
            ReturnXML += "<token>" + accessToken + "</token>";
            ReturnXML += "</Item>";   
            
            ReturnXML = "<Envelope><Body><Result>" + ReturnXML + "</Result></Body></Envelope>";
            results.dom.LoadXml(ReturnXML);         
        }
    }
        
    return results;
}

protected Report[] GetReports(string token)
{
     string powerBIReportsUrl = "https://api.powerbi.com/beta/myorg/reports";
    
    HttpWebRequest request = System.Net.WebRequest.Create(powerBIReportsUrl) as System.Net.HttpWebRequest;
    request.KeepAlive = true;
    request.Method = "GET";
    request.ContentLength = 0;
    request.ContentType = "application/json";

    // Add token to the request header
    request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

    // Get HttpWebResponse from the GET request
    using (HttpWebResponse httpResponse = request.GetResponse() as System.Net.HttpWebResponse)
    {
        // Use StreamReader to deserialize the HttpWebResponse
        using (StreamReader reader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
        {
            string responseContent = reader.ReadToEnd();

            var results = Newtonsoft.Json.JsonConvert.DeserializeObject<Reports>(responseContent);
            return results.value;
        }
    }
}

protected Dashboard[] GetDashboards(string token)
{
     string powerBIDashboardsUrl = "https://api.powerbi.com/beta/myorg/dashboards";
    
    HttpWebRequest request = System.Net.WebRequest.Create(powerBIDashboardsUrl) as System.Net.HttpWebRequest;
    request.KeepAlive = true;
    request.Method = "GET";
    request.ContentLength = 0;
    request.ContentType = "application/json";

    // Add token to the request header
    request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

    // Get HttpWebResponse from the GET request
    using (HttpWebResponse httpResponse = request.GetResponse() as System.Net.HttpWebResponse)
    {
        // Use StreamReader to deserialize the HttpWebResponse
        using (StreamReader reader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
        {
            string responseContent = reader.ReadToEnd();

            var results = Newtonsoft.Json.JsonConvert.DeserializeObject<Dashboards>(responseContent);
            return results.value;
         
        }
    }
}

// Used to deserialize the response
public class Reports
{
    public Report[] value { get; set; }
}
public class Report
{
    public string id { get; set; }
    public string name { get; set; }
    public string webUrl { get; set; }
    public string embedUrl { get; set; }
  
}

public class Dashboards
{
    public Dashboard[] value { get; set; }
}
public class Dashboard
{
    public string id { get; set; }
    public string displayName { get; set; }
    
// note: the missing curly brace is intentional due to the way 
// Aras executes method code inside a template method

以上是关于csharp power BI博客的示例代码的主要内容,如果未能解决你的问题,请参考以下文章

Microsoft Power BI Desktop概念学习系列之Microsoft Power BI Desktop的官网自带示例数据(图文详解)

Power BI DAX 度量计算

在 Power Bi 中使用 Power Query M 的上一周日期

无法获取客户端凭据访问令牌来授权 Power BI

Power BI - 未找到表的列 'Column1' 错误

Power BI Python 在Power BI Desktop中Python代码如何使用Power Query数据