Click here to Skip to main content
15,867,568 members
Articles / Hosted Services / Azure

FTP to CRM using Azure Function

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
27 May 2018CPOL2 min read 3.5K  
FTP to CRM using Azure Function

FTPTOCRM

New to Azure Function?

Function is serverless offering form Azure. Serverless computing is a way to write code without the need to manage infrastructure, application dependencies and other required resources. Even for scaling, Azure will take care of them. Once we know the environment using Function is easy, login to Azure portal write or deploy code and start using it.

An Azure function is simply a function written in C#, Java, JavaScript, F#, Python or PHP. A function can be executed manually or scheduled to run automatically. The third way to execute a function is through triggers. A trigger can be another Azure service or something which has no link with Azure. Some Azure services which can trigger a function are Cosmos DB, Event Hubs, App Service, Storage queues, blobs, Service Bus queues or topics. Functions are also available for Logic Apps, Power Apps, Microsoft Flow and outside Azure over HTTP.

About Demo

This function will be scheduled to run once in 24 hours. From FTP server, it will read a CSV file (data for lead entity) and pass it to CRM. Functions can be coded and published from Visual Studio or directly in Azure portal, for this demo, I will use the later approach.

FtpFun_0CreatefunApp

Create Function App and Function

To create a function, we need to create a function app. If you don’t want to use existing resource group and storage, feel free to create new.

Image 3

Image 4

Open function app and add a function in it:

Image 5

Image 6

Image 7

In Schedule add 0 0 12 * * *. This cron expression will trigger this function in midnight at 12 o’clock. See this for more details about cron expression.

Image 8

Image 9

App Settings

Let’s add CRM connection string, FTP URL and credentials as application settings to avoid hard coding.

Image 10

Image 11

Sample of application settings:

Key Value Comments
FtpId UserId Ftp account user id
FtpPassword <a>P@ssword</a> Ftp account password
FtpAddress  ftp://ftp.domain.com/full.csv Ftp address with file name
Connectionstring AuthType = Office365; Url = https://crminstace.crm6.dynamics.com/;

UserName=crmuser@domain.com;

Password=crmP@ssword

This is example with Dynamics 365, no need to surround with single or double quote.

Code

FtpFun_10CreatefunApp

C#
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
}

Adding Dependencies

Since code use assemblies from CRM SDK, we will add these to our code. With Azure Function, it is achieved using project.json file. Add project.json file if it is not already there and then add NuGet packages in it.

FtpFun_11CreatefunApp

FtpFun_12CreatefunApp

JavaScript
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.CrmSdk.CoreAssemblies": "9.0.0.0",
"Microsoft.CrmSdk.XrmTooling.CoreAssembly":"9.0.0.7″
}
}
}
}

Let’s save it, run the function if it is not already running and see how we go.

Result

FtpFun_13CreateFunApp

Hope this help, enjoy your 365 day. 🙂

History

  • 28th May 2018: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
Developer

Comments and Discussions

 
-- There are no messages in this forum --