Skip to content

Console Application Example

Ekhor edited this page Apr 10, 2019 · 16 revisions

The following example demonstrates how to use the Solution Audit tool from a .NET Console application.

  • Open Visual Studio
  • From Visual Studion menu select File > New > Project

Add New VS Project

  • Add a new .NET Framework Console App (ensure Console is at least .NET 4.6.2)

.NET Console App

  • Install the Nuget Package named Capgemini.Xrm.Audit.Reports

Add NuGet To Solution Audit

  • Install the Nuget Package named Microsoft.CrmSdk.XrmTooling.CoreAssemblies

Add NuGet To Solution Audit

  • Confirm that Capgemini.Xrm.Audit.Reports and Microsoft.CrmSdk.XrmTooling.CoreAssemblies are successfully installed

  • Supply a valid Dynamics 365 connection string through the app.config. Please note this is for demonstration purpose only. A more robust approach should be taken to ensure security of Dynamics 365 credentials

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
    <add name="Crm" connectionString="[ENTER A VALID DYNAMICS 365 CONNECTION STRING]" />
</configuration>
  • Add a reference to System.Configuration. This will enable the console app to read the application settings from the app.config file

Add Reference To System Consiguration

  • Replace the existing code in the Program.cs file with the code snippet below:
using Capgemini.Xrm.Audit.Core.Interfaces;
using Capgemini.Xrm.Audit.Core.Loggers;
using Capgemini.Xrm.Audit.DataAccess.Repos;
using Capgemini.Xrm.Audit.Reports;
using Capgemini.Xrm.Audit.Reports.Generators;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Capgemini.Xrm.SolutionAudit.Console
{
    class Program
    {
        private static readonly string _downloadsPath = 
                                             Path.Combine(Environment.GetFolderPath( 
                                                          Environment.SpecialFolder.UserProfile),
                                                          "Downloads");
        private static string _crmConnectionString;
        private static ILogger _logger;

        static void Main(string[] args)
        {
            [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;

            var _logger = new ConsoleLogger();
            var repository = new SolutionRepository(new CrmServiceClient(CrmConnectionString), _logger);
            var solutionAuditor = new CrmAuditor(repository, _logger);
            var publisherFilter = new List<string> { "Dynamics 365" };

            var crmInstance = solutionAuditor.AuditCrmInstance(InstanceName, publisherFilter);
            
            //Generate report in Microsoft Excel format
            var excelReport = new ExcelReport(_downloadsPath);
            excelReport.SaveSolutionAudit(crmInstance);

            //Generate report in XML format
            var xmlReport = new XmlReport(_downloadsPath);
            xmlReport.SaveSolutionAudit(crmInstance);

            //Generate report in HTML format
            var htmlReport = new HtmlReport(_downloadsPath);
            htmlReport.SaveSolutionAudit(crmInstance);

            //Generate report in JSON format
            var jsonReport = new JsonReport(_downloadsPath);
            jsonReport.SaveSolutionAudit(crmInstance);
        }

        private static string CrmConnectionString
        {
            get
            {
                return _crmConnectionString = _crmConnectionString ??
                                              ConfigurationManager.ConnectionStrings["Crm"].ConnectionString;
            }
        }

        protected static string InstanceName
        {
            get
            {
                var stringArray = CrmConnectionString.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                var firstItem = stringArray.First(x => x.ToLower().Trim().StartsWith("url="));
                var instanceName = firstItem.ToLower().Trim().Replace("url=", "").Replace("https://", "");
                return instanceName;
            }
        }
    }
}
  • Run the console app. This will produce the following file within the Downloads folder. Note that from the code snippet above that it is possible to specify a different output folder for the reports.

JSON Report

For this report, the entire solution audit output is returned as as a JSON file and a sample can be downloaded here.

Below is a sample sample screenshot

Sample JSON Report

HTML files

For this report, the solution audit output is produced via two HTML files.

The first of the HTML file is Home which displayes all the solutions contained within the Dynamics instance. Sample screenshot is shown below:

Sample Overview Report

The second generated HTML file is Entities which displayes all the Entities contained within the solutions listed in home HTML report. Sample screenshot is shown below:

Sample Entities Report

Microsoft Excel file

For this report, the solution audit output is produced via a Microsoft Excel Spreadsheet. This spreadsheet lists the solutions found during the audit and their associated components through worksheets.

A sample screenshot is shown below:

Sample Excel Report

XML file

For this report, the entire solution audit output is returned as as an XML file and a sample is can be downloaded here.

Below is a sample sample screenshot

Sample XML Report

Clone this wiki locally