Unable to download annotation attachment throwing Exception

365 Views Asked by At

I am creating a workflow activity to download the annotation attachment from a custom entity. Running the workflow throws

System.NullReferenceException: Object reference not set to an instance of an object.

Here is my code:

private static string SaveFile(string fileName, string noteBody)
{
    string outputFileName = @"C:\temp\" + fileName;

    if (!string.IsNullOrEmpty(noteBody))
    {
         // Download the attachment in the current execution folder.
         byte[] fileContent = Convert.FromBase64String(noteBody);
         System.IO.File.WriteAllBytes(outputFileName, fileContent);

    }
    else
    {
        throw new InvalidPluginExecutionException("File content is empty or cannot be retrieved");
    }

    return outputFileName;
}

Debugging it shows that the error is thrown by:

System.IO.File.WriteAllBytes(outputFileName, fileContent);

From Visual Studio I get this exception

Request for permission of type System.Security.Permissions.FileIOPermission

1

There are 1 best solutions below

0
Arun Vinoth-Precog Tech - MVP On

CRM online plugins & workflows must be registered & run in sandbox isolation mode, which is secured & partially trusted in cloud (not Full trust).

Hence you cannot access C:\temp\ in Server from Plugin/workflow code. You will get FileIOPermission exception.

Developers have the option of registering their plug-ins in the sandbox, known as partial trust, or outside the sandbox, known as full trust. Full trust is supported for on-premises and Internet-facing Microsoft Dynamics 365 deployments. For a Microsoft Dynamics 365 (online) deployment, plug-ins or custom workflow activities must be registered in the sandbox (partial trust)

Access to the file system, system event log, certain network protocols, registry, and more is prevented in the sandbox.

Read more