Publish a report using the Microsoft Dynamics 365 web service

 

Updated: November 29, 2016

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

To publish a report to Microsoft Dynamics 365, you can either create a new report record or update an existing report record. Use the IOrganizationService.Create method to create a report, or use the IOrganizationService.Update method to update a report. You must specify the report type, report visibility, report category, and the related entities for the report as shown below.

When you create report record, specify the type of report using the Report.ReportTypeCode attribute. A report can be one of the following types:

  • Reporting Services Report: For this report type, set a value for the Report.BodyText attribute. Also, specify the name of the .rdl file that contains the report definition by using the Report.Filename attribute.

  • Other Report: For this report type, set a value for the Report.BodyBinary attribute. Specify the name of the .rdl file that contains the report definition by using the Report.Filename attribute.

  • Linked Report: For this report type, set a value for the Report.BodyUrl attribute.

To show and run a report in the different report categories, such as Marketing, Sales, or Service, use the ReportCategory.CategoryCode attribute to specify a category. You can specify multiple categories for a report.

By default, the report is visible in the All Reports, Including Sub-Reports view in the Reports grid. To show a report in additional views in the grid, or different areas, such as the entity form or entity grid, use the ReportVisibility.VisibilityCode attribute to specify the view or the area. You can specify multiple views and areas for a report.

To specify a related entity for the report, use the ReportEntity.ObjectTypeCode attribute. You can specify multiple related entities.

This sample shows how to create the records needed to publish a report.




// Define an anonymous type to define the possible values for
// report type.
var ReportTypeCode = new
{
    ReportingServicesReport = 1,
    OtherReport = 2,
    LinkedReport = 3
};

// Define an anonymous type to define the possible values for
// report category.
var ReportCategoryCode = new
{
    SalesReports = 1,
    ServiceReports = 2,
    MarketingReports = 3,
    AdministrativeReports = 4
};

// Define an anonymous type to define the possible values for
// report visibility
var ReportVisibilityCode = new
{
    ReportsGrid = 1,
    Form = 2,
    Grid = 3,
};

// Instantiate a report object.
// See the Entity Metadata topic in the SDK documentation to determine
// which attributes must be set for each entity.

Report sampleReport = new Report
{
    Name = "Sample Report",
    BodyText = File.ReadAllText("SampleReport.rdl"),
    FileName = "SampleReport.rdl",
    LanguageCode = 1033, // US English
    ReportTypeCode = new OptionSetValue(ReportTypeCode.ReportingServicesReport)
};
// Create a report record named Sample Report.
_reportId = _serviceProxy.Create(sampleReport);


// Set the report category.
ReportCategory sampleReportCategory = new ReportCategory
{
    ReportId = new EntityReference(Report.EntityLogicalName, _reportId),
    CategoryCode = new OptionSetValue(ReportCategoryCode.AdministrativeReports)
};
_reportCategoryId = _serviceProxy.Create(sampleReportCategory);

// Define which entity this report uses.
ReportEntity reportEntity = new ReportEntity
{
    ReportId = new EntityReference(Report.EntityLogicalName, _reportId),
    ObjectTypeCode = Account.EntityLogicalName
};
_reportEntityId = _serviceProxy.Create(reportEntity);


// Set the report visibility.
ReportVisibility rv = new ReportVisibility
{
    ReportId = new EntityReference(Report.EntityLogicalName, _reportId),
    VisibilityCode = new OptionSetValue(ReportVisibilityCode.Form)
};
_reportVisibilityId1 = _serviceProxy.Create(rv);

rv = new ReportVisibility
{
    ReportId = new EntityReference(Report.EntityLogicalName, _reportId),
    VisibilityCode = new OptionSetValue(ReportVisibilityCode.Grid)
};
_reportVisibilityId2 = _serviceProxy.Create(rv);

rv = new ReportVisibility
{
    ReportId = new EntityReference(Report.EntityLogicalName, _reportId),
    VisibilityCode = new OptionSetValue(ReportVisibilityCode.ReportsGrid)
};
_reportVisibilityId3 = _serviceProxy.Create(rv);

Console.WriteLine("{0} published in Microsoft Dynamics CRM.", sampleReport.Name);

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright

Community Additions

ADD
Show: