0

我正在使用 Dynamics 365 的在线实例。我创建了 C# Web API 来从活动实体导出数据。当我从视觉工作室运行它时,它工作正常。

现在我想在 Dynamics 365 中单击按钮时调用它。要求是当用户单击按钮时,应调用 Web API 并导出数据。我不知道如何完成这项任务。谁能帮我解决这个问题。请为我提供完成此任务的步骤。Web API 代码如下

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.ServiceModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Sdk;
using System.Windows.Forms;

namespace Experiments
{
    class Program
    {
        private static OrganizationService _orgService;
        static void Main(string[] args)
        {
            try
            {
                CrmConnection connection = CrmConnection.Parse(ConfigurationManager.ConnectionStrings["CrmOnline"].ConnectionString);

                using (_orgService = new OrganizationService(connection))
                {

                    var exportToExcelRequest = new OrganizationRequest("ExportToExcel");
                    exportToExcelRequest.Parameters = new ParameterCollection();
                    //Has to be a savedquery aka "System View" or userquery aka "Saved View" 
                    //The view has to exist, otherwise will error out
                    exportToExcelRequest.Parameters.Add(new KeyValuePair<string, object>("View", new EntityReference("savedquery", new Guid("{00000000-0000-0000-00AA-000010001902}"))));
                    exportToExcelRequest.Parameters.Add(new KeyValuePair<string, object>("FetchXml", @"<?xml version='1.0'?>
                    <fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>
                       <entity name='activitypointer'>
                          <attribute name='subject'/>
                          <attribute name='ownerid'/>
                          <attribute name='prioritycode'/>
                          <attribute name='regardingobjectid'/>
                          <attribute name='activitytypecode'/>
                          <attribute name='statecode'/>
                          <attribute name='scheduledstart'/>
                          <attribute name='scheduledend'/>
                          <attribute name='activityid'/>
                          <attribute name='instancetypecode'/>
                          <attribute name='community'/>
                          <attribute name='senton'/>
                          <attribute name='statuscode'/>
                          <order descending='false' attribute='scheduledend'/>
                          <filter type='and'>
                             <condition attribute='actualdurationminutes' value='43800' operator='le'/>
                          </filter>
                          <link-entity name='systemuser' alias='activitypointerowningusersystemusersystemuserid' link-type='outer' visible='false' to='owninguser' from='systemuserid'>
                              <attribute name='internalemailaddress'/>
                          </link-entity>
                          <link-entity name='email' alias='email_engagement' link-type='outer' visible='false' to='activityid' from='activityid'><attribute name='isemailfollowed'/>
                             <attribute name='lastopenedtime'/> 
                             <attribute name='delayedemailsendtime'/>
                          </link-entity>
                       </entity>
                   </fetch>"));
                    exportToExcelRequest.Parameters.Add(new KeyValuePair<string, object>("LayoutXml", @"
                    <grid name='resultset' object='2' jump='fullname' select='1' icon='1' preview='1'>
                        <row name='result' id='activitypointerid'>
                            <cell name='activitytypecode' width='150' />
                            <cell name='statecode' width='112' />
                            <cell name='scheduledstart' width='110' />
                            <cell name='scheduledend' width='110' />
                        </row>
                    </grid>"));
                    //need these params to keep org service happy
                    exportToExcelRequest.Parameters.Add(new KeyValuePair<string, object>("QueryApi", ""));
                    exportToExcelRequest.Parameters.Add(new KeyValuePair<string, object>("QueryParameters",new InputArgumentCollection()));
                    var exportToExcelResponse = _orgService.Execute(exportToExcelRequest);
                    if (exportToExcelResponse.Results.Any())
                    {
                       File.WriteAllBytes("Activities.xlsx", exportToExcelResponse.Results["ExcelFile"] as byte[]);
                    }
                }
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                string message = ex.Message;
                throw;
            }
        }
    }
}

Thanks.
4

2 回答 2

1

我建议您将 C# 代码编译为操作,并将其作为工作流程中的一个步骤。请参阅此处了解如何从工作流中调用自定义操作。

然后,我建议您安装Ribbon Workbench。这将允许您自定义表单和导航以添加一个或多个按钮。您可以使用工作台自定义这些按钮,将它们的命令设置为调用您的工作流,进而调用您的操作(您的 C# 代码)。


请注意,有几种解决方案可以实现您的要求,我只是建议了一种。其他解决方案可能包括 JavaScript 和调用 Web API 客户端。

于 2018-05-22T07:21:19.973 回答
0

由于您在线使用 Dynamics 365,因此我建议您使用 CDS 连接器创建 Microsoft FLOW 应用程序。希望这可以帮助。

于 2019-02-05T21:15:05.860 回答