ArcGIS Pro 3.6 API Reference Guide
ArcGIS.Desktop.Reports Namespace / ReportFactory Class / CreateReport Method / CreateReport(String,ReportCustomTemplateDefinition) Method
The name of the report.
T:ArcGIS.Desktop.Reports.ReportCustomTemplateDefinition object defining data sources, fields, maps, charts and supplemental pages to populate template.
Example

In This Topic
    CreateReport(String,ReportCustomTemplateDefinition) Method
    In This Topic
    Creates a new report using a custom template and adds it to a project. The source data for the report can be either a Layer or Table or a data connection to the data. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    Public Overloads Function CreateReport( _
       ByVal name As String, _
       ByVal templateDataDef As ReportCustomTemplateDefinition _
    ) As Report

    Parameters

    name
    The name of the report.
    templateDataDef
    T:ArcGIS.Desktop.Reports.ReportCustomTemplateDefinition object defining data sources, fields, maps, charts and supplemental pages to populate template.

    Return Value

    The created report.
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Template data is invalid.
    Example
    Create report
    //Note: Call within QueuedTask.Run()
    //The fields in the datasource used for the report
    //This uses a US Cities dataset
    var listFields = new List<CIMReportField> {
              //Grouping should be the first field
              new CIMReportField{Name = "STATE_NAME", FieldOrder = 0, Group = true, SortInfo = FieldSortInfo.Desc}, //Group cities using STATES
              new CIMReportField{Name = "CITY_NAME", FieldOrder = 1},
              new CIMReportField{Name = "POP1990", FieldOrder = 2, },
          };
    //Definition query to use for the data source
    var defQuery = "STATE_NAME LIKE 'C%'";
    //Define the Datasource
    var reportDataSource = new ReportDataSource(featureLayer, defQuery, listFields);
    //The CIMPage defintion - page size, units, etc
    var cimReportPage = new CIMPage
    {
      Height = 11,
      StretchElements = false,
      Width = 6.5,
      ShowRulers = true,
      ShowGuides = true,
      Margin = new CIMMargin { Bottom = 1, Left = 1, Right = 1, Top = 1 },
      Units = LinearUnit.Inches
    };
    
    //Report template
    var reportTemplates = await ReportTemplateManager.GetTemplatesAsync();
    var reportTemplate = reportTemplates.Where(r => r.Name == "Attribute List with Grouping").First();
    
    //Report Styling
    var reportStyles = await ReportStylingManager.GetStylingsAsync();
    var reportStyle = reportStyles.Where(s => s == "Cool Tones").First();
    
    //Field Statistics
    var fieldStatisticsList = new List<ReportFieldStatistic> {
              new ReportFieldStatistic{ Field = "POP1990", Statistic = FieldStatisticsFlag.Sum}
              //Note: NoStatistics option for FieldStatisticsFlag is not supported.
          };
    var report = ReportFactory.Instance.CreateReport("USAReport", reportDataSource, cimReportPage, fieldStatisticsList, reportTemplate, reportStyle);
    Create a report from a custom report template
    //Note: Run within QueuedTask.Run()
    await QueuedTask.Run(() =>
    {          
      // get the first map in the project
      MapProjectItem item = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(n => n.Name == "Cities");
      if (item == null)
        return;
    
      Map map = item.GetMap();
      if (map == null)
        return;
    
      Layer cityLayer = map.FindLayers("U.S. Cities")[0];
      if (cityLayer == null)
        return;
    
      // load template information
      var templatePath = System.IO.Path.Combine($@"{Project.Current.HomeFolderPath}\TemplateSimpleReportWithTokens.rptt");
      ReportCustomTemplateDefinition templateDef = ReportTemplateManager.GetCustomTemplateDefinition(templatePath);
      if (templateDef == null || templateDef.ReportTemplateDataSourceItems?.Count() < 1)
        return;
    
      // get template info of first data source (parent) and set its data source
      ReportCustomTemplateDataSourceInfo parentDataSourceInfo = templateDef.ReportTemplateDataSourceItems.ElementAt(0);
    
      parentDataSourceInfo.SetReportDataSource(cityLayer);
    
      TemplateTokenFieldInfoValues templateTokenFieldInfoValues = parentDataSourceInfo.TokenInfoValues;
    
      parentDataSourceInfo.DefinitionQuery = "STATE_NAME = 'Massachusetts'";
      templateTokenFieldInfoValues["[text-field-value-0]"] = "CITY_NAME";
      templateTokenFieldInfoValues["[text-field-name-0]"] = "City";
      templateTokenFieldInfoValues["[numeric-field-value-0]"] = "POP1990";
      templateTokenFieldInfoValues["[numeric-field-name-0]"] = "Population";
    
      if (templateDef.CanCreateReport())
      {
        // create the report
        report = ReportFactory.Instance.CreateReport("RelateSiblingsFromCode", templateDef);
      }
    });
    //UI thread
    if (report != null)
    {
      //open report
      await FrameworkApplication.Panes.CreateReportPaneAsync(report);
    }
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3.6 or higher.
    See Also