Get a file from a plugin in Microsoft Dynamics 365 - c#

I´ve installed Dynamics Version 1612 (8.2.2.112) (DB 8.2.2.112) on-premises. I wrote a plugin to connect to a web API, work perfect, now I need to add more situations. The API can return an url to a zipped file with the response in a JSON file. Well, there're any change to point me in the right direction? First, to get the file. STFW so far he gave me no clue. Thanks in advance.

If you have an URL then get unzip and process.
Example how to get and unzip file.
WebClient wc = new WebClient();
using (MemoryStream stream = new MemoryStream(wc.DownloadData("URL")))
{
using (var zip = new ZipArchive(stream, ZipArchiveMode.Read))
{
...
}
}

Related

Copy File to remote windows server using .net core

I have put endpoint that accept the zip file. The endpoint is working fine to get the zip file. Now from that endpoint after I get that file I am trying to copy that file to a different remote Server.
I try below code to connect to remote Server using the below code by referring to micrsoft docs https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.runimpersonated?view=net-6.0:
bool returnValue = LogonUser(#"ACC\Test.test", "acc.local", "password",
9, 0,
out safeAccessTokenHandle);
if (returnValue)
{
WindowsIdentity.RunImpersonated(safeAccessTokenHandle, () =>
{
//Destination remote server folder to upload file
var pathToUploadFile = #"\\152.158.100.45\D\FileUpload";
string fileName;
fileName = file.FileName;
var path = Path.Combine(pathToUploadFile, fileName);
using (var stream = new FileStream(path, FileMode.Create))
{
//save a zip file to folder
file.CopyTo(stream);
}
});
}
In this code I get error of username and password is incorrect in this line of code:
var stream = new FileStream(path, FileMode.Create)
I am not even sure if the LogonUser method connects to a remote server. So is there is better way to do this copy file to remote server Please give an idea to implement in .net core 6.0 C#. Any help is really appreciated. Thanks
Finally I am able to solve the issue by implementing the Win32 API called WNetUseConnection solutions that was mentioned on below questions:
Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials

How to download file from google drive in c#?

I am trying to download file from Google Drive using ASPSnippets.GoogleAP.dll and Google.Apis.Drive.v3.dll. But facing some challenges. File is being downloaded, but it is in some type of weird HTML content.
I am using following code to download it :
GoogleConnect.ClientId = "xxxx.apps.googleusercontent.com";
GoogleConnect.ClientSecret = "xxxxxx";
GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];
GoogleConnect.API = EnumAPI.Drive;
if (!string.IsNullOrEmpty(Request.QueryString["code"]))
{
string code = Request.QueryString["code"];
string json = GoogleConnect.Fetch("me", code);
GoogleDriveFiles files = new JavaScriptSerializer().Deserialize<GoogleDriveFiles>(json);
//Remove the deleted files.
var driveService = new DriveService();
Label1.Text = theHiddenField1.Value;
WebClient wb = new WebClient();
wb.DownloadFile(theHiddenField1.Value, "C://" + fileName);
}
else if (Request.QueryString["error"] == "access_denied")
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
}
else
{
GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.readonly");
}
Can someone please help me to resolve the issue?
I'm not a C# developer, but if you're using Google.Apis.Drive.v3, then that is the latest version of the Google Drive API. I don't see any part of your code that references the Google APIs Client Library for .NET, but that's the recommended way to talk to modern Google APIs.
If you've installed it, take a look at the C# quickstart sample for the Drive API. Then check out the C#/.NET examples on the Download Files page in the docs, and it should lead you to a working solution. In your other comment, you asked about passing auth tokens, so if you're using the client library, you shouldn't have to worry about that part. On that docs page, you'll find a sample for regular file downloads and another for exporting Google documents (Docs, Sheets, Slides).
Finally, for additional reference, here are the .NET reference docs for the Drive API and the .NET Google APIs Client Library developers guide.

Download files using FluentFTP

I am trying to implement FTP transfer using FluentFTP in C#. Getting a directory listing is very easy, but I am stuck on downloading files.
I found one article that has an example in its comments here but it won't compile because I cannot find where the class FtpFile comes from.
Does anybody have an example of how tocan I download a file from an ftp server using FluentFTP ?
EDIT: I found some examples here https://github.com/hgupta9/FluentFTP But there is no example on how to actually download a file.
In the this article Free FTP Library there is an example but it does not compile. This is the example
FtpClient ftp = new FtpClient(txtUsername.Text, txtPassword.Text, txtFTPAddress.Text);
FtpListItem[] items = ftp.GetListing();
FtpFile file = new FtpFile(ftp, "8051812.xml"); // THIS does not compile, class FtpFile is unknown
file.Download("c:\\8051812.xml");
file.Name = "8051814.xml";
file.Download("c:\\8051814.xml");
ftp.Disconnect();
EDIT: The solution
The article I found contained an example that set me in the wrong direction.
It seems there was once a Download method but that is gone long ago now.
So the answer was to let that go and use the OpenRead() method to get a stream and than save that stream to a file.
There are now DownloadFile() and UploadFile() methods built into the latest version of FluentFTP.
Example usage from https://github.com/robinrodricks/FluentFTP#example-usage:
// connect to the FTP server
FtpClient client = new FtpClient();
client.Host = "123.123.123.123";
client.Credentials = new NetworkCredential("david", "pass123");
client.Connect();
// upload a file
client.UploadFile(#"C:\MyVideo.mp4", "/htdocs/big.txt");
// rename the uploaded file
client.Rename("/htdocs/big.txt", "/htdocs/big2.txt");
// download the file again
client.DownloadFile(#"C:\MyVideo_2.mp4", "/htdocs/big2.txt");

Download Latest File from SharePoint Server

I have very little idea on sharepoint. Need a start,how to access the share point site and download the latest file avilable in a particular folder.
I am using ASP.Net/C#.Net and Visual Studio 2008 to download file.
What is requirement means any particular libraray/update/pathes?
How can i get to know path of webservice ? (I only know the direct URL)
Is it possible to access the metadata of the file.
Any help will be appreciated.
Thanks
I found this on a web search at some point. This is what I ended up using.
var remotefile = #"https://pathtoremotefile";
var localfile = #"C:\pathtolocalfile";
using(WebClient wc = new WebClient())
{
wc.Credentials = CredentialCache.DefaultNetworkCredentials;
// or new System.Net.NetworkCredential("user","pass","domain");
wc.DownloadFile(remotefile,localfile);
}

Index pdf documents in Solr from C# client

Basically I'm trying to index word or pdf documents in Solr and found the ExtractingRequestHandler, but can't figure out how to write code in c# that performs the HTTP POST request like in the Solr wiki: http://wiki.apache.org/solr/ExtractingRequestHandler.
I've installed Solr 3.4 on Tomcat 7 (7.0.22) using the files from the example/solr directory in the Solr zip and I haven't altered anything. The ExtractingRequestHandler should be configured out of the box in the solrconfig.xml and ready to use, right?
Can some of you give an C# (HttpWebRequest) example of how you make the HTTP POST request and upload a PDF file like it is done using curl in the Solr wiki?
I've look all over this site and many others trying to find an example or a tutorial on how this is done, but haven't found anything.
EDIT:
I finally managed to get it to work using SolrNet!
In order for it to work you need to copy this to a lib-folder in your Solr installation directory from the Solr zip:
apache-solr-cell-3.4.0.jar file from the dist folder
content of contrib\extraction\lib directory
With SolrNet 0.4.0 beta 2, this code does the job:
Startup.Init<IndexDocument>("YOUR-SOLR-SERVICE-PATH");
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<IndexDocument>>();
using (FileStream fileStream = File.OpenRead("FILE-PATH-FOR-THE-FILE-TO-BE-INDEXED"))
{
var response =
solr.Extract(
new ExtractParameters(fileStream, "doc1")
{
ExtractFormat = ExtractFormat.Text,
ExtractOnly = false
});
}
solr.Commit();
Sorry for the trouble. I hope however that others will find this useful.
I would recommend using the SolrNet client. It supports the ExtractingRequestHandler.

Categories