Share via


Publish a report using the Microsoft Dynamics 365 web service

 

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.

Specify report type

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.

Specify report category

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.

Specify report visibility

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.

Specify report entities

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

Example

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);


' Define an anonymous type to define the possible values for
' report type.
Dim ReportTypeCode = New With {
     Key .ReportingServicesReport = 1,
     Key .OtherReport = 2,
     Key .LinkedReport = 3}

' Define an anonymous type to define the possible values for
' report category.
Dim ReportCategoryCode = New With {
     Key .SalesReports = 1,
     Key .ServiceReports = 2,
     Key .MarketingReports = 3,
     Key .AdministrativeReports = 4}

' Define an anonymous type to define the possible values for
' report visibility
Dim ReportVisibilityCode = New With {
     Key .ReportsGrid = 1,
     Key .Form = 2,
     Key .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.

Dim sampleReport As Report =
 New Report With {
  .Name = "Sample Report",
  .BodyText = File.ReadAllText("SampleReport.rdl"),
  .FileName = "SampleReport.rdl",
  .LanguageCode = 1033,
  .ReportTypeCode = New OptionSetValue(ReportTypeCode.ReportingServicesReport)
 }
' Create a report record named Sample Report.
_reportId = _serviceProxy.Create(sampleReport)


' Set the report category.
Dim sampleReportCategory As ReportCategory =
 New ReportCategory With {
  .ReportId = New EntityReference(Report.EntityLogicalName, _reportId),
  .CategoryCode = New OptionSetValue(ReportCategoryCode.AdministrativeReports)
 }
_reportCategoryId = _serviceProxy.Create(sampleReportCategory)

' Define which entity this report uses.
Dim reportEntity As ReportEntity =
 New ReportEntity With {
  .ReportId = New EntityReference(Report.EntityLogicalName, _reportId),
  .ObjectTypeCode = Account.EntityLogicalName
 }
_reportEntityId = _serviceProxy.Create(reportEntity)


' Set the report visibility.
Dim rv As ReportVisibility =
 New ReportVisibility With {
  .ReportId = New EntityReference(Report.EntityLogicalName, _reportId),
  .VisibilityCode = New OptionSetValue(ReportVisibilityCode.Form)
 }
_reportVisibilityId1 = _serviceProxy.Create(rv)

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

rv = New ReportVisibility With {
 .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)

See Also

Developers guide to reports for Microsoft Dynamics 365
Publish reports
Sample: Publish a report
Manage a report in offline mode

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright