如何在动态365中使用C#Web API

时间:2018-05-22 07:08:43

标签: dynamics-365-sales

我正在使用动态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.

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

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