如何在Dynamics 365中使用C#Web API

问题描述 投票:0回答:2

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

现在,我想在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.
dynamics-crm dynamics-crm-365 dynamics-365-sales
2个回答
1
投票

我建议您将C#代码编译为Action,并将其包括在工作流程中。有关如何从工作流程中调用自定义操作的信息,请参见here

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


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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.