Business Process Error
System.ArgumentException: Illegal characters in path. at System.IO.Path.GetFileName(String path) at System.IO.File.InternalWriteAllBytes(String path, Byte[] bytes, Boolean checkHost)at RetrieveAttachments.RetrieveClass.Execute(IServiceProvider serviceProvider)
The code is as follows:
QueryExpression notes = new QueryExpression { EntityName = "annotation", ColumnSet = new ColumnSet("filename", "subject", "annotationid", "documentbody","mimetype") };
notes.Criteria.AddCondition("annotationid", ConditionOperator.Equal, annotationid);
EntityCollection NotesRetrieve = service.RetrieveMultiple(notes);
if (NotesRetrieve != null && NotesRetrieve.Entities.Count > 0)
{
foreach (var note in NotesRetrieve.Entities)
{
string fileName = note.GetAttributeValue<string>("filename");
//string fileType = note.GetAttributeValue<string>("mimetype");
FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write, "D:\\note");
string fileLocation = f+ fileName;
byte[] fileContent = Convert.FromBase64String(NotesRetrieve.Entities[0].Attributes["documentbody"].ToString());
System.IO.File.WriteAllBytes(fileLocation, fileContent);
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.ToString());
}
Santosh,
You can use following code to download attachments from Note entity in Dynamics CRM online.
code for .cs file:
Code for .config file:
You just need to edit .config file code, this should work for you.
Note:you have to create C# console application and add required assemblies from Nuget.
This line is part of you culprit:
string fileLocation = f+ fileName;
This line is going to create an invalid file name because
FileIOPermission.ToString()
returns an XML fragment.Here is an example to fix that issue, clean the filename of illegal characters, and use
Path.Combine
to build a valid file path.If the above means you are trying to use a CRM plugin to write data to your local machine - as opposed to the CRM server - then this will never work.
The plugin runs on the CRM server so it wont have access to your client machine. E.g. when the plugin runs its looking for
D:\note
on the CRM server - not on your personal computer.If you want to download files to your local machine you are better of creating a console application that runs on your local machine.
In any case there is an example of attachment downloading here.
filePath
in your case would beD:\note
(this still won't work in a plugin though).CRM Online supports only Sandbox mode that means it isolates the code, You can't access outside environment in Sandbox mode which in your case is you local computer drive.
To achieve your goal you can create a console application as James Wood have said, that will write files into your local machine.