コード例 #1
0
        /// <summary>
        /// Create and configure the organization service proxy.
        /// Download the report definition.
        /// Optionally delete any entity records that were created for this sample.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete)
        {
            try
            {
                // Connect to the Organization service.
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    // Call the method to create any data that this sample requires.
                    CreateRequiredRecords();


                    // Query for an existing report: Account Overview. This is a default report in Microsoft Dynamics CRM.
                    QueryByAttribute reportQuery = new QueryByAttribute(Report.EntityLogicalName);
                    reportQuery.AddAttributeValue("name", "Account Overview");
                    reportQuery.ColumnSet = new ColumnSet("reportid");

                    // Get the report.
                    EntityCollection retrieveReports = _serviceProxy.RetrieveMultiple(reportQuery);

                    // Convert retrieved Entity to a report
                    Report retrievedReport = (Report)retrieveReports.Entities[0];
                    Console.WriteLine("Retrieved the 'Account Overview' report.");

                    // Use the Download Report Definition message.
                    DownloadReportDefinitionRequest rdlRequest = new DownloadReportDefinitionRequest
                    {
                        ReportId = retrievedReport.ReportId.Value
                    };

                    DownloadReportDefinitionResponse rdlResponse = (DownloadReportDefinitionResponse)_serviceProxy.Execute(rdlRequest);

                    // Get the current directory path.
                    _currentDirectoryPath = Directory.GetCurrentDirectory();

                    // Access the xml data and save to disk
                    XmlTextWriter reportDefinitionFile = new XmlTextWriter(_currentDirectoryPath + "\\NewReport.rdl", System.Text.Encoding.UTF8);
                    reportDefinitionFile.WriteRaw(rdlResponse.BodyText);
                    reportDefinitionFile.Close();

                    if (File.Exists(_currentDirectoryPath + "\\NewReport.rdl"))
                    {
                        Console.WriteLine("Downloaded the report definition (NewReport.rdl) to '{0}'.", _currentDirectoryPath.ToString());
                    }


                    DeleteRequiredRecords(promptForDelete);
                }
            }
            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }
コード例 #2
0
        static string ReportDownload(string xml, Entity e)
        {
            DownloadReportDefinitionResponse resp = null;

            try {
                var req = new DownloadReportDefinitionRequest()
                {
                    ReportId = e.Id,
                };

                resp = OrgService.Execute(req) as DownloadReportDefinitionResponse;
            }
            catch (Exception ex) {
                Console.WriteLine("Report not supported " + ex.Message);
                return(null);
            }

            return(XmlPretty(resp.BodyText, null));
        }