Sending Attachment to SAP B1 remote Service Layer - c#

In a C# application, I want to send files to "remote SAP B1 service layer". I have tried many ways to send an attachment to this remote service layer, using their b1s/v1/Attachments2 endpoint, but I continuously receive an error "A file with this name already exists. [Attachments2_Lines.FileName][line: 1], ". Even this file does not exists in the service layer shared directory.
Moreover, I do have the read/write permissions in the destination path.
Any Kind of help will be highly appreciated.

I had a similiar issue when adding attachments to a document. Try to send a GET and retrieve all records for /Attachments2. If the file was added before there might already exist a record having the same filename.
To add it again, you need to call a DELETE on the specific ID first, then POST it again.

Related

Rename files using SFTP

there is a project assigned to me that create some files and send those files using SFTP to some server. there is another programme to read and send that files to another place(the files first programme sent to the server). it checks continuously new files from the server and read it and send. but the problem is sometimes the second program read files not completely imported to that server. that cause system to crash. they told me to do change in the first program before sending files, rename the files first and send and after finished upload rename again the sent files, files that in the server. is this possible to or is a there better way to do this. is there anyone have some ideas I'm kindly asking to share with me
That it's a good synchronization method, I mean using a temporary name during the transfer and the rename at once at the end.
The implementation depends on which approach you've used in the program.
It should be something like this:
// Rename the file or directory:
success = sftp.RenameFileOrDir("oldFilename.txt","newFilename.txt");
if (success != true) {
Console.WriteLine(sftp.LastErrorText);
return;
}
Basically:
Catch the event of transfer completed
Request the RENAME command

File copy fails within a service when trying to access a mapped drive

I am trying to copy a file using File.Copy() from my Windows Service (running as Network Service).
File.Copy(sourceFile, targetFile, true);
Trouble is, when the target is on a mapped network drive, I get an error:
Could not find a part of the path 'Z:\copiedfile.txt'
I found someone else who had this problem in 2006, but that thread has an unhappy ending: the OP gave up and found a workaround.
Have any solutions been discovered in the past 7 years?
Mappings are a per user session item, which means that while the mapping may exist on your desktop it does not exist in the service. You will need to use the UNC path instead to copy the file.
A service should not directly access local or network resources
through mapped drive letters. Additionally, a service should not use
the WNetXXXXXXX APIs to add, remove, or query any mapped drive
letters. Although the WNetXXXXXXX APIs may return successfully, the
results will be incorrect. A service (or any process that is running
in a different security context) that must access a remote resource
should use the Universal Naming Convention (UNC) name to access the
resource.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/f9a702da-724b-4acc-a1bb-ac4d225838c8/copy-file-to-a-mapped-drive
http://support.microsoft.com/kb/180362/en-us

C# Upload Files on another partition of the server

I'm using FileUpload.SaveAs() function of C# to upload files to the server but I want to save the files on another partition. Let us say, save the files on Drive D of the server instead on the current drive which is Drive C. Please share your thoughts. Thanks is advance.
I have learned that using full path such as
FileUpload.SaveAs("D:\FileUpload");
will save the file outside the web server.
Check this out.
To simplify the question, how can I upload files on the other partition of the server that hosts my web app?
Based on the documentation from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas.aspx, the String filename is the full path name of the location to save. Meaning you should be able to do so e.g:
FileUpload.SaveAs("D:\where_you_want_to_save")
By the way what have you tried and what error did you get?
Looking at the example on MSDN, it would appear that .SaveAs() accepts a fully qualified file name as a parameter. You could potentially use a Path object to cleanly build a path for the file, or just specify one directly as a string:
uploader.SaveAs("d:\\someFolder\\someFile.ext");
Resolved this by using Virtual Directory of IIS and providing admin credentials for authentication

How do I send `MailMessage` that was stored in a `SpecifiedPickupDirectory`

I am storing Emails in a Directory, like the following. How do I go about, programatically, sending those at a later time, say an event of some sorts
smtpClient.PickupDirectoryLocation = "C:\\EmailHoldingBin\\";
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
Files stored using the PickupDirectoryLocation are written in raw SMTP (MIME/EML) format and are meant to be processed by the local SMTP server which is typically IIS. While the SmtpClient and the MailMessage objects can effectively "write" as raw SMTP/MIME they have no provision for reading such files. If you must read these files you'll need to write your own parser or use one already created such as this one.
I agree with the answer from gangelo
Another point:
The Pickup directory only works if there is some service (like IIS) picking up the mail messages stored there and sends them - so you need to configre IIS for this to work...
EDIT - Exchange pickup folder as per comment:
for example with Exchange 2007
http://www.msexchange.org/articles_tutorials/exchange-server-2007/management-administration/exchange-pickup-folder.html
and for Exhange 2010 http://technet.microsoft.com/en-us/library/bb124230.aspx
You need to call smtpClient.Send(System.Net.Mail.MailMessage) but not before setting From, To, Subject and Body properties of System.Net.Mail.MailMessage.

Is FileInfo.Copy accross network good solution? Is CAS required?

Seems that everything I do involves win services copying files across servers. I seem to get a lot of security & securityaccess type exceptions and never fully understand the causes. I am wondering if fileinfo or file.copy is a good solution or if there is a better. Is there a particular attribute I should be using or something to avoid these errors? Issue is not account or password related. example are \ipaddress\sharename \ipaddress\drive\path using domain accounts.
---Added Specific example.---
- I log on to serverA as domain\username. (including domain name)
- I open file eplorer in the address bar enter \\serverB\c$\folder hit enter, I right-click, create new file. No problem.
- I install service, go to properties select Log On, This Account and set the username as domain\username (including the domain name) same password I logged onto serverA with. It accepts it no problem.
Application does a FileSystemWatcher on \\serverA and copy to \\serverB when changed to keep the config files in sync.
private void CopyNewFileToClone()
{
FileInfo OriginalConfigFile = new FileInfo(Path.Combine(ConfigurationManager.AppSettings.Get("directoryToWatch"), ConfigurationManager.AppSettings.Get("fileToWatch")));
FileInfo CloneConfigFile = new FileInfo(Path.Combine(ConfigurationManager.AppSettings.Get("directoryToCopyTo"), ConfigurationManager.AppSettings.Get("fileToCopyTo")));
FileInfo tmp = new FileInfo(Path.Combine(CloneConfigFile.DirectoryName,"~" + CloneConfigFile.Name));
OriginalConfigFile.CopyTo(tmp.FullName, true);
tmp.CopyTo(CloneConfigFile.FullName, true);
tmp.Delete();
}
When I start the service I get Service cannot be started.
System.UnauthorizedAccessException: Access to the path '\\serverB\C$\folder\filename' is denied.
I use fileinfo to copy files from servers and it seems to work fine. If your sure it's not a account or password issue I would start looking at your DNS. If the network can't resolve what account is trying to access the network folder it won't matter if you are using a valid account. You may get lucky some/most of the time with cached accounts but there is no telling when it might not work and when it will work.
I would trace the network if you are getting a lot of broadcast messages for failed responses.
This was due to Code Access Security policy. Ran
c:\Windows\Microsoft.NET\Framework\v2.0.50727\CasPol.exe -af <path\application.exe>
and error resolved.
Adding as a installation step in all apps that need to write to HD, especially accross network via unc such as \server\share\file.

Categories