使用 Azure Function 进行 FTP 到 CRM





5.00/5 (1投票)
使用 Azure Function 进行 FTP 到 CRM
不了解 Azure Function?
Function 是 Azure 提供的无服务器服务。无服务器计算是一种无需管理基础设施、应用程序依赖项和其他所需资源即可编写代码的方式。即使是扩展,Azure 也会负责处理。一旦熟悉了 Function 的环境,登录到 Azure 门户,编写或部署代码,即可开始使用。
Azure Function 只是用 C#、Java、JavaScript、F#、Python 或 PHP 编写的一个函数。该函数可以手动执行,也可以安排自动运行。执行函数的第三种方式是通过触发器。触发器可以是另一个 Azure 服务,也可以与 Azure 无关的任何事物。可以触发函数的 Azure 服务包括 Cosmos DB、Event Hubs、App Service、存储队列、Blob、Service Bus 队列或主题。Function 也适用于 Logic Apps、Power Apps、Microsoft Flow 以及 Azure 外部的 HTTP 请求。
关于演示
此函数将安排在 24 小时内运行一次。它将从 FTP 服务器读取 CSV 文件(潜在客户实体的数据),并将其传递到 CRM。Function 可以从 Visual Studio 或直接在 Azure 门户中编码和发布,对于此演示,我将使用后者方法。
创建 Function 应用和 Function
要创建函数,我们需要创建一个函数应用。如果您不想使用现有的资源组和存储,请随时创建新的。
打开函数应用并在其中添加一个函数
在计划中添加 0 0 12 * * *。此 cron 表达式将在午夜 12 点触发此函数。请参阅此处了解有关 cron 表达式的更多详细信息。
应用设置
为了避免硬编码,让我们将 CRM 连接字符串、FTP URL 和凭据作为应用程序设置添加。
应用程序设置示例
键 | 值 | 注释 |
FtpId | UserId | Ftp 帐户用户 ID |
FtpPassword | <a>P@ssword</a> | Ftp 帐户密码 |
FtpAddress | ftp://ftp.domain.com/full.csv | 包含文件名的 FTP 地址 |
Connectionstring | AuthType = Office365; Url = https://crminstace.crm6.dynamics.com/;
| 这是一个 Dynamics 365 的示例,无需用单引号或双引号括起来。 |
代码
using System;
using System.Configuration;
using System.IO;
using System.Net;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info($”Execution Started. : {DateTime.Now} “);
Stream fileStream = null;
string[] fileContentToWriteToCRM;
IOrganizationService org;
string ftpId = ConfigurationManager.AppSettings[“Ftpid”].ToString();
string ftpAddress = ConfigurationManager.AppSettings[“ftpAddress”].ToString();
string ftpPassword = ConfigurationManager.AppSettings[“ftpPassword”].ToString();
#region Read Ftp File(s)
FtpWebRequest ftpReq =
(System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpAddress);
ftpReq.Credentials = new NetworkCredential(ftpId, ftpPassword);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ftpReq.EnableSsl = false;
WebResponse tmpRes = ftpReq.GetResponse();
fileStream = tmpRes.GetResponseStream();
#endregion
#region ProcessData
TextReader tmpReader = new StreamReader(fileStream);
var txtData = tmpReader.ReadToEnd();
fileContentToWriteToCRM = txtData.Split(new string[]
{ “\r\n” }, StringSplitOptions.RemoveEmptyEntries);
#endregion
#region CRM Data Posting
string connectionstring = ConfigurationManager.AppSettings[“connectionstring”];
CrmServiceClient conn =
new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionstring);
org = (IOrganizationService)conn.OrganizationWebProxyClient !=
null ? (IOrganizationService)conn.OrganizationWebProxyClient :
(IOrganizationService)conn.OrganizationServiceProxy;
log.Info($”CRM connection established”);
log.Info($”Looping to move data to CRM”);
foreach (var row in fileContentToWriteToCRM)
{
var rowvalues = row.Split(‘,’);
Entity lead = new Entity(“lead”);
lead.Attributes[“subject”] = rowvalues[0].ToString();
lead.Attributes[“firstname”] = rowvalues[1].ToString();
lead.Attributes[“lastname”] = rowvalues[2].ToString();
var id = org.Create(lead);
log.Info($”Lead created in CRM with GUID : {id} “);
}
log.Info($”Loop Ended moved all data to CRM “);
#endregion
}
添加依赖项
由于代码使用 CRM SDK 中的程序集,我们将这些添加到我们的代码中。使用 project.json 文件可以实现 Azure Function。如果还没有 project.json 文件,请添加它,然后在其中添加 NuGet 包。
{
“frameworks”: {
“net46”:{
“dependencies”: {
“Microsoft.CrmSdk.CoreAssemblies”: “9.0.0.0”,
“Microsoft.CrmSdk.XrmTooling.CoreAssembly”:”9.0.0.7″
}
}
}
}
让我们保存它,运行该函数(如果尚未运行),看看效果如何。
结果
希望这有帮助,享受您的 365 天。
历史
- 2018 年 5 月 28 日:初始版本