Sunday 29 April 2012

Microsoft Dynamics CRM 2011 - Develop Fetch XML Based SSRS Reports In Visual Studio 2008


Microsoft Dynamics CRM 2011 supports two kinds of custom SSRS reports. One is the old way, using filtered views. Other is the new way, using Fetch XML. The previous version, Microsoft Dynamics CRM 4.0 only supported reports based on filtered views. These filtered views were directly accessed writing TSQL.


Microsoft Dynamics CRM 2011 Online has few differences from its on-premises cousin. One of the differences is we cannot have filtered views based SSRS reports. Microsoft Dynamics CRM 2011 Online only supports Fetch XML based SSRS reports. The reason being you don't have direct access to SQL Server on CRM 2011 Online. This is totally opposite to the on-premises version where you have total control and visibility over your CRM databases. So CRM 2011 on-premises support both filtered views and Fetch XML based SSRS reports, but CRM 2011 Online supports only Fetch based SSRS reports.

Fetch is a proprietary query language that is used in Microsoft Dynamics CRM. It is based on a schema that describes the capabilities of the language. The Fetch XML language supports similar query capabilities as query expression.

The Fetch XML based reports are created the same way, using SQL Server 2008 R2 BIDS (Business Intelligence Development Studio) or Visual Studio 2008. BIDS is an IDE (integrated development environment) having only SSRS report templates, whereas Visual Studio 2008 has all the project templates including SSRS project templates. You also need an additional component for creating Fetch XML based SSRS reports. Microsoft Dynamics CRM 2011 Report Authoring Extension is required to author custom Fetch-based reports for Microsoft Dynamics CRM by using Business Intelligence Development Studio.

Download this component from: Microsoft Dynamics CRM 2011 Report Authoring Extension

In this blog I will create a simple Fetch XML based SSRS report in Visual Studio 2008. I am assuming you know how to upload the report file (*.rdl) into CRM 2011 and run the report in CRM.

1) I will develop a report on two custom entities. One custom entity is Movie and the other is Genre. The report will output the list of movies based on the genre selection. Genre records will be listed as a pick list (option set) report parameter.


The movies will be shown which belong to the selected Genre in the pick list report parameter.


Shown below is the screen shot of movie records in CRM.

Movie Records

2) Below is the screen shot for genre records. The genre is to categorise movies. For example a movie can belong to a 'Family' genre or 'Thriller' genre, based on the story and content. Movies have N:1 relationship with Genres.

Genre Records

3) I will use Visual Studio 2008, to create a SSRS reports solution. I will then create a fetch based report in this solution.

Visual Studio 2008

4) In Visual Studio, click on File > New > Project. This will open a new project window.

Create a New Project

5) In the new project window, go to section "Business Intelligence Projects". Select the project type as "Report Server Project". This SSRS project type will create a blank solution. I can then add a new report with data sources, datasets and queries.

Report Server Project






6) After the project is created add the first item, a Report. Click on Reports > Add > New Item. This will open an "Add New Item" window.

Add a New Report

7) In the "Add New Item" window, select a file of type Report. I will give this report a name "Movies by Genre".

This option will create an empty report file with the extension of rdl (report definition language). 

Selecting a type of Report

8) In the screenshot below, you can see the report "Movies by Genre.rdl" has been created.

Movies by Genre Report

9)  In the report, I will add a data source. This is a connection to my SQL Server CRM database.

Click on Data Sources > Add Data Source. This will open a "Data Source Properties" window. 

Add a Data Source

10) Shown below is the "Data Source Properties" window. Give a name and define a connection to the SQL Server CRM database.

I have named my data source as "SQLConnection". In the embedded connection, select the type as "Microsoft Dynamics CRM Fetch".

In the connection string, I cannot specify a direct connection to my SQL Server CRM database. I have to specify the CRM Organization for which this report will be running. Click OK and a data source is created by the name "SQLConnection".

Data Source Properties

11) I will create a dataset for Genres. Since it is a Fetch XML based report, the query to get genres will be a Fetch XML. It cannot be a TSQL using filtered views.

As I mentioned above, I will have Genre records as a pick list report parameter. Based on the Genre selection, the movie records belonging to that Genre will be shown.

For my blog I will simply download the Fetch XML created automatically by advanced find. In that way I can also show you how to get Fetch XML from advanced find. You don't have to create Fetch XML from scratch. You can have Fetch XML automatically created from advanced find, and then modify if needed as per your needs.

I will go my CRM interface in Microsoft Internet Explorer. I will go to Genres records. I will click "Advanced Find" ribbon button on top right corner. This will open the Advanced Find window.

Fetch XML for Genre

 12) In the Advanced Find window by default there is a condition of status equals active. We can add more conditions. For my report this condition is enough. I will click Download Fetch XML ribbon button on the top right corner.

Download Fetch XML for Genre

13) This will give me an option to either open or save the file. I will select Save As, to save this file on my desktop. I will save this file with the name "GenreFetchXML.xml".

"Save As" Fetch XML for Genre

14) I will go to my desktop and open the file "GenreFetchXML.xml" in notepad.

Fetch XML File for Genre
  
15) As we can see the Fetch XML is there and ready to be used. If we need to modify the query, we can simply do it in notepad or use any text-based editor.

I want the Genre query to retrieve only Name and the GUID, so that I can create a report parameter with a pick list of all Genres. The Fetch XML has 2 additional fields "createdon" and "createdby".  I will remove these 2 fields.

Just on the side note, as seen in the Fetch XML, the field names are logical names and not schema names. Logical names are equal to schema names, but always in lowercase. For more information on logical names and schema names please read my blog:
http://ashishmahajancrm.wordpress.com/2012/03/15/microsoft-dynamics-crm-2011-entity-logical-name-and-entity-schema-name

Fetch XML File for Genre, in Notepad

After removing "createdon" and "createdby", the final Fetch XML is shown as below. I will copy this Fetch XML and paste it in the Genre data set, which I am creating next.

Final Genre Fetch XML

16) I will go to my reports project in visual studio. I will select Datasets and right click. I will select "Add Dataset" to create a new dataset.


Add a New Dataset for Genres

17) This will open a "Dataset Properties" window. I will name my Genre dataset as "GetGenres". I will select option of embedded dataset. I will select the data source as "SQLConnection".

I will paste the copied Genre Fetch XML in the query field. This query is of type Text. Select OK to create this dataset.

Dataset Properties for Genres

18) As seen below the Genres dataset is created with the name "GetGenres".

GetGenres Dataset

19) Now I will create a report parameter, based on the Genres dataset. Select Parameters and right click to add a new parameter.

Add a Parameter for Genre

20) This will open a "Report Parameter Properties" window. I will name the parameter as "Genre". I will give the prompt as "Select Genre". The data type is text.

Genre Report Parameter Properties - General


21) In the "Available Values", select the option "Get values from a query". We will define the source of this report parameter, which in my case is the Genres dataset. I will put "GetGenres" in dataset. The report parameter value will be the genre GUID and the label will be the genre name.

Genre Report Parameter Properties - Available Values


22) In the "Default Values" select the option of "No default value". I don't want the report to have any pre-selected default genre. Click OK to save the report parameter.

Genre Report Parameter Properties - Default Values

23) As seen below a report parameter by the name "Genre" is created.

Genre Parameter Created


24) I will create my second dataset which is for Movies. This dataset will feed the display for the report. Again I will take the same approach of creating a Fetch XML for movies through advanced find.

I will go to my CRM Movies records. I will click on Advanced Find ribbon button on top right.

Fetch XML for Movie

25) This will open the Advanced Find window. I will remove the non-required fields. This I can achieve by clicking on "Edit Columns".

Edit Columns for Movie Fetch XML

26) This will open the "Edit Columns" window. I will remove the last three columns "Number in Series", "Created On" and "Created By", as I don't need them. Select each one of the three columns and click "Remove" button. Click OK.

Remove non-required Columns for Movie Fetch XML

27) After I have removed the non-required columns, I will download the Fetch XML. I will click "Download Fetch XML" ribbon button on top right.

Download Movie Fetch XML

28) This will give me an option to either open or save the file. I will select Save As, to save this file on my desktop. I will save this file with the name "MovieFetchXML.xml".

"Save As" Fetch XML for Movie


29) I will go to my desktop and open the file "MovieFetchXML.xml" in notepad.

Movie Fetch XML File

30) As you can see the Movie Fetch XML has the columns which I selected. I will copy this Movie Fetch XML and use it in the Movie dataset.

Movie Fetch XML in Notepad

31) Go to Datasets and right click to add a new Dataset.

Add a New Dataset for Movies

32) This will open a "Dataset Properties" window. This is the same way we created Genre dataset. I will name the new dataset as "GetMovies". I will select the data source as "SQLConnection". In Query I will paste the Movie Fetch XML, which I had copied earlier. Click OK.

Dataset Properties for Movies

33) As seen below, "GetMovies" dataset is created.

GetGenres and GetMovies

34) I will add a table to my report. This table is a layout where my movie records will be displayed.

I will click Toolbox, on the left side, to expand it. I will select a table, drag and drop on to the report's designer.

Add a Table

35) As seen below a table is created. The table has 2 rows (header and data) and 3 columns by default. I can add or delete data rows and columns.

Table

36) I will add the dataset "GetMovies" to the "DataSetName" property of the table. This will bind the "GetMovies" dataset to the table so that the data rows can display the records from FetchXML.

Select a DataSet for the Table

37) I will finalise designing my table. For simplicity, I will display only two columns, the Movie GUID and the Movie Name. I have removed the third column. I have formatted the table header with a yellow background and aligned header to the center.

In the table data row, for first column I will click on upper right corner and select new_movieid. I will do the same for second column (as shown below) and select new_name. These field options appear automatically as I have already set the DataSetName property of the table equal to "GetMovies" dataset.

I have completed designing my report. I can go ahead and test it.

Configure Table for Display
 
38) Select the Preview tab in Visual Studio. This tab is used to run a SSRS report created in Visual Studio. We should always test the report before we upload it to CRM. This way we can remove any issues before uploading the report to CRM.

In the preview tab, as shown below, I can see a parameter labelled "Select Genre". The parameter lists all the Genres. I will select Family genre and click "View Report" button.

Testing the Report


39) As seen below the report has produced output. It shows all the movies whose genre is family.

You can format your Fetch XML based report as per your requirements. You can upload this Fetch XML based report the usual way into your CRM 2011 environment.

Report Output

In this blog we saw how easy it is to design a Fetch XML based SSRS report in Visual Studio 2008. You can create a report on a simple Fetch XML as I did above or you can use as complex Fetch XML as you like.

Remember, there is a Download Fetch XML button provided so that we can have ready-made queries. Microsoft has done a great job in giving us this option of downloading Fetch XML and saving us a lot of time.

I hope this blog about 'Microsoft Dynamics CRM 2011 - Develop Fetch XML Based SSRS Reports In Visual Studio 2008' was informative. Please feel free to leave your comments.

14 comments:

  1. A good and and very informative article on CRM.It is very illustrative and provide all the details about the CRM and its usage.All the tasks are shown with figures and are very illustrative.

    ReplyDelete
  2. Thanks James.

    I am glad the blog was informative to you.

    cheers,

    ReplyDelete
  3. How do you combine multiple datasets into one table/matrix? I see that you used genres as a parameter, but how would you include it alongside the movie data? I can't seem to find a way to do this, meaning you have to do multiple link-entities to get related records.

    ReplyDelete
    Replies
    1. Hi,

      The UNION operator in SQL allows you to combine two or more select statements and return the result as part of a single DataSet.

      The current fetch-xml schema does not have an operator that allows a similar UNION functionality. Instead, the workaround is to retrieve the results as part of different data sets.

      Suppose you want names of all accounts and contacts in you organization. In a fetch based report, you would instead have two data sets one corresponding to each of the above, but using the same data source.

      You can now use both these data sets. You can insert 2 tables to your report. Attach account data set to first table and contact data set to second table to output both accounts and contacts.

      You can show these 2 tables one below another. Or you can display data in a sub report.

      I hope this helps.

      Delete
  4. Hi Ashish,

    Can you use 2 data sources when making this report? For example, we have the embedded Dynamics CRM Fetch connection. Is it possible for me to also use a shared data source in the report that will pull info from our GP Dynamics tables?

    I have tried this but it results in an Invalid Datasource error when i try to run it from CRM. Any ideas on why this happens?

    ReplyDelete
    Replies
    1. Hi,

      For the reports you upload to Microsoft Dynamics CRM, you cannot have shared data sources. It has to be embedded connection.

      Yes, you can have many data connections (data sources) embedded into your single report.

      Delete
    2. Hi,

      To expand my answer. The reason you cannot have shared data sources is because shared data sources in a Visual Studio "reports project" is not part of the report. It is created outside a report and can be shared among many reports. The report will execute fine in your Visual Studio with shared data sources, but when you upload your report to Dynamics CRM, it should have embedded data sources.

      Delete
    3. Hi Ashish,

      I will try this tomorrow. Thank you for the quick response!

      -AJ

      Delete
    4. Hi Ashish,

      I am trying to get different result within one data set. like "Total incident", total open incident, "total closed incident etc. Is it possible to have all the result in one data set.

      Delete
  5. Hey thanks for writing your review on Microsoft Dynamics CRM 2011 Application. This was really informative and helpful for me. 
    CRM Development

    ReplyDelete
  6. Hi Ashish,
    good job by have a little question more.
    In fact, if i don't select any specific Genre, i would to see in the Genre combo-box an adding voice (ALL for example) that let me to see all the Movies of all the Genres.
    In a few word, i need to define this adding voice like an Attribute and when it's null, show all the Movies.
    Someone could help me, please?
    Thanks
    Luca

    ReplyDelete
  7. Thank you so much for sharing. If there are already some fetch xml based report in CRM and I want to modify the code, how can I download them?

    Thanks!

    ReplyDelete
  8. Dear Ashis,
    Can you please confirm whether these fetch xml based custom reports will work on sql server data bases other than organisation database?

    ReplyDelete