C# Wrapper for Power BI REST API


UPDATE 2017-05-18:
I released a new version of this project and also published it on GitHub: https://github.com/gbrueckl/PowerBI.API.Client
A blog post which refers to the updates can be found here.


Since the last major update last year, Power BI offers some APIs which can be used to interact with content and also data that is stored in Power BI. Microsoft provides a good set of samples on how to use the APIs on GitHub and also a an interactive APIARY web-UI which you can use to build and test API calls on-the-fly. However, it can still be quite cumbersome as you have to deal with all the REST API calls and the returned JSON on your own. So I decided to write a little C# Wrapper where you simply pass in your Azure AD Application Client ID and you can deal with all Object of the Power BI API as they were regular C# objects.

Here is a little example on how to list all available reports and get the EmbedURL of a given tile using the PowerBIClient:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using pmOne.PowerBI;
using pmOne.PowerBI.PowerBIObjects;

namespace SampleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            PowerBIClient pbic = new PowerBIClient(“ef4aed1a-9cab-4bb3-94ea-ffffffffffff”);

            Console.WriteLine(“Available Reports:”);
            foreach(PBIReport pbir in pbic.Reports)
            {
                Console.WriteLine(pbir.Name);
            }

            Console.WriteLine();
            Console.WriteLine(“Get EmbedURL for Tile [Retail Analysis Sample].[This Year’s Sales]”);
            Console.WriteLine(pbic.GetDashboardByName(“Retail Analysis Sample”).GetTileByName(“This Year’s Sales”).EmbedURL);

            Console.WriteLine(“Press <Enter> to exit …”);
            Console.ReadLine();
        }
    }
}

As you can see, its pretty simple and very easy to use, even for non-developers. You can find all the source-code and the sample application for download below. The code as I have written it is very likely not the best code possible, but it works for my needs, is straight forward, simple and saves me a lot of work and time when dealing with the PowerBI API. Also, if the API changes, you may need to adopt the code accordingly. However, for the future I hope that Microsoft provides some metadata so that VisualStudio can build all this code automatically using e.g. Swagger. But for the time being feel free to use, improve or extend my code Smile

SourceCode: PowerBIClient_Source.zip

21 Replies to “C# Wrapper for Power BI REST API”

    • yes, I have seen it. it looks very good and is probably a much better code than mine. But to be fully honest, its too advanced for me (and I guess also to must other users) and I like to understand the code that I am using in my projects:)

      and as I said, I still hope for MS to publish the API metadata so we can use VS to generate all this code for us automatically …

  1. I’m investigating the generation of a data feed to Azure Power BI, along with the Azure Power BI parameters that are required to generate a graph in Azure Power BI.

    A series of questions will be presented to the user using a set of rules that determine the next question(s) to be presented — these questions will determine which data (held in Azure SQL) is to be used by Azure Power BI to generate a graph.

    The parameters of the graph will be included as part of the feed to Azure Power BI, or the feed will select a pre-saved template in Azure Power BI for the graph to be generated.

    I would be interested to view any cases where something like this has been achieved.

      • Hi

        I want the Same in C# Code but with different parameters like Start Date and End Date.I have done this on power bi desktop with Manage parameter functionality but Now I Want to pass these parameters dynamically from C# Code. Can you please update me for the same if you have any idea.

        Thanks in Advance.

        • So you basically have a PowerBI Desktop template with parameters (StartDate/EndDate) and want to do the following via C#:
          1) load the template
          2) set the parameters
          3) upload it to the PBI service
          4) refresh it online with the previously set parameters
          so far correct?

          The REST API is only here to interact with the service and not with any desktop component so steps 1) and 2) will not work. However, steps 3) and 4) will definitly work via the API

          regards,
          -gerhard

          • Hi,
            Actually In power bi I am using stored procedure and that procedure needs Start Date and End Date to execute. So I pass that parameters to stored procedure using Manage Parameter. Now I want to integrate this dynamic report with web. From Web I want to pass these parameter to execute Stored procedure and get result.

            If you know any other way to do this please let me know. I really need this asap.

            Thanks.

            • Well, you cannot trigger a parameterized refresh through a query from the UI.
              You could only try a live connection instead and use views instead of procedures. However, performance will be less good but it should work for your scenario

  2. Hi Gerhard, I am very new to this. Your post is really helpful.

    I have a quick question: How can we automate data push into Power BI Service?

    • Hi Rishabh,
      sure, you can can also automate the Pushing of data into the PowerBI Service
      There are a lot of tutorials out there – here are just some links:
      https://powerbi.microsoft.com/en-us/blog/power-bi-apis-real-time-example/
      https://msdn.microsoft.com/library/mt203561.aspx
      https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-walkthrough-push-data/

      what exactly do you need to automate?

      Also, there have been some recent changes to the API so you need to ensure that the wrapper is still working:
      https://powerbi.microsoft.com/en-us/blog/newdatasets/

      hope that helps,
      -gerhard

      • Just to give you a background, we have hit an issue with Power BI on Dynamics CRM Online. We are getting data from Dynamics CRM Online using Fetch XML . However, we are able to fetch only 5000 records using FetchXML due to its limitation. I read somewhere that we can use Use FetchXML with a paging cookie to fetch the complete dataset. This includes writing C# code.

        So, I was wondering if we can write c# code to get complete data from Dynamics CRM using FetchXML with paging cookie and get the data to be refreshed on a scheduled basis.

        • I’d say that you can achieve the same also with PowerQuery – have you tried this yet?
          I would not use the Push API unless you really need real-time data and your scenario seems to be more like a workaround

          • We are currently trying to extracts data from CRM online using fetchxml.
            To overcome limit of 5000 records we are trying to make smaller extracts of data based on date ranges.
            For example we have monthly extracts for all cases and we are passing that condition(based on dates) as part of fetchxml.
            The data runs well in power BI desktop . However, it faces refresh issue in Power BI online.

            We are thinking to have a workaround. Please do let us know if there are any pointers to alternative

            • As I said in my previous comment, I would rather try to write a PowerQuery/M Script to load the data which executes smaller batches and then combines the data in Power BI

  3. Hello – I have a organization published content pack. I need to be reading the dataset to get some data that i need in my D365 CRM system. Is there a way to read it using the REST API ? I tried to access it and ended up seeing a 404 not found error. I saw at different forums that reading the actuial data from the datasets using API is currently not supported, but the posts were old. Any idea if it still is not supported ?

    • Hi Priya,

      there is no way to access data via the API as for now and I also not aware of any plans to expose any data via the API.
      Theoretically, you can access the data via DAX/MDX similar to – for example – Mirosoft Excel or PowerBI Desktop (referencing a dataset in the PowerBI Service)

      regards,
      -gerhard

Leave a Reply to Gerhard Brueckl Cancel reply

Your email address will not be published. Required fields are marked *

*