Quantcast
Viewing latest article 2
Browse Latest Browse All 2

Consume a Dot Net class in MS Dynamics NAV 2009 SP1 R2

Here I like to explain how to consume a .net class in Dynamics NAV 2009 SP1 R2.Task 1: Create a .net dll to consume in NAV.

  1. Open Visual Studio 2008 and select Class Library project from the installed project templates. Enter the project name you like.

Image may be NSFW.
Clik here to view.

2. Copy the following codes.

using

System;

using

System.Collections.Generic;

using

System.Linq;

using

System.Text;

using

System.IO;

using

System.Net;


namespace

ProColFTP

{

publicclassProCol_FTP

{

string ftpaddress = “”, filetoupload = “”, userid = “”, password = “”, filetodownload = “”, filename2 = “”, errodesc = “”;

string[] filelist;

publicstring FTPAddress

{

get { return (ftpaddress); }

set

{

ftpaddress =

value;

}

}

publicstring FileToUpload

{

get { return (filetoupload); }

set { filetoupload = value;  }

}

publicstring FileToDownload

{

get { return (filetodownload); }

set

{filetodownload =

value;}

}

publicstring Downloadtofilename

{

get { return (filename2); }

set

{filename2 =

value;}

}

publicstring UserId

{

get { return (userid); }

set

{userid =

value; }

}

publicstring Password

{

get { return (password); }

set

{ password =

value; }

}

publicstring ErrorDesc

{

get { return (errodesc); }

set { errodesc = value; }

}

publicbool UploadFile(string remotefilename)

{

errodesc =

“”;

try

{

FileInfo toUpload = newFileInfo(@filetoupload);

//string uploadfilename = @ftpaddress + “/” + toUpload.Name;

string uploadfilename = @ftpaddress + “/” + remotefilename;

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uploadfilename);

request.Method =

WebRequestMethods.Ftp.UploadFile;

request.Credentials =

newNetworkCredential(userid, password);

Stream ftpstream = request.GetRequestStream();

FileStream file = File.OpenRead(@filetoupload);

int length = 1024;

byte[] buffer = newbyte[length];

int bytesRead = 0;

do

{

bytesRead = file.Read(buffer, 0, length);

ftpstream.Write(buffer, 0, bytesRead);

}

while (bytesRead != 0);

file.Close();

ftpstream.Close();

return (true);

}

catch (Exception e1)

{

errodesc = e1.Message;

return (false);

}

}

publicbool DownloadFile()

{

errodesc =

“”;

try

{

WebClient request = newWebClient();

request.Credentials =

newNetworkCredential(userid, password);

byte[] fileData = request.DownloadData(ftpaddress + “/” + FileToDownload);

FileStream file = File.Create(@Downloadtofilename);

file.Write(fileData, 0, fileData.Length);

file.Close();

return (true);

}

catch (Exception e1)

{

errodesc = e1.Message;

return (false);

}

}

publicbool DeleteFile(string filetodelete)

{

try

{

string deletefilename = @ftpaddress + “/” + filetodelete;

FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(deletefilename);

ftpReq.Credentials =

newNetworkCredential(userid, password);

ftpReq.Method =

WebRequestMethods.Ftp.DeleteFile;

FtpWebResponse ftpRes = (FtpWebResponse)ftpReq.GetResponse();

return (true);

}

catch (Exception e1)

{

return (false);

}

}

publicbool IsFileExist(string Filename)

{

FTPFileList();

for (int i = 0; i < filelist.Length; i++)

{

if (filelist[i] == null) break;

if (filelist[i].ToLower()==Filename.ToLower())

return(true);

}

return(false);

}

privatevoid FTPFileList()

{

filelist =

null;

filelist =

newstring[10000];

//string foldername = @ftpaddress + “\” + folder_name”;

string foldername = @ftpaddress;

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(foldername);

request.Credentials =

newNetworkCredential(userid, password);

request.Method =

WebRequestMethods.Ftp.ListDirectory;

StreamReader streamReader = newStreamReader(request.GetResponse().GetResponseStream());

string fileName = streamReader.ReadLine();

int i = 0;

while (fileName != null)

{

fileName = streamReader.ReadLine();

filelist[i] = fileName;

i++;

}

request =

null;

streamReader =

null;

}

}

}

Build the solution and copy the dll file into Classic\Add-ins folder and Server\Add-ins folder.

Create a new navision Code Unit and define a DotNet data type variable.

Image may be NSFW.
Clik here to view.
DefineDotNet Type Variable

DefineDotNet Type Variable

Click on SubType and click on Assembly. You can see your dll file that you copied on Client\Add-ins over the screen. Select the file and Click Ok.

Image may be NSFW.
Clik here to view.

If you view C/AL Symbol Menu (F5), you can see the class properties, Methods and Constructors…

The following code sets the Class property and call the methods.

The ‘FileToUpload’ method uploads the file into UploadDir folder and this class can be used to upload/download files to/from a ftp server and can used in Navision 2009 SP1 R2 RTC pages.

Hope this will help


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing latest article 2
Browse Latest Browse All 2

Trending Articles