-3

Business Process Error System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.CodeAccessPermission.Demand() at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.File.InternalWriteAllBytes(String path, Byte[] bytes, Boolean checkHost) at RetrieveAttachments.RetrieveClass.Execute(IServiceProvider serviceProvider) The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.FileIOPermission The Zone of the assembly that failed was: MyComputer

Plug-In code is followed:

        try
          {

            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 cleanFileName = string.Empty;
                       foreach (var chr in fileName.ToCharArray().ToList())
                       {
                           if (!Path.GetInvalidFileNameChars().Contains(chr)) cleanFileName = cleanFileName + chr;
                       }
                       FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, @"D:\note");      
                       writePermission.Demand();
                       string filePath = Path.Combine(@"D:\note", cleanFileName);
                       byte[] fileContent = Convert.FromBase64String(NotesRetrieve.Entities[0].Attributes["documentbody"].ToString());
                       System.IO.File.WriteAllBytes(filePath, fileContent);


                 }

             }
           }
           catch (Exception ex)
           {
            throw new InvalidPluginExecutionException(ex.ToString());
           }
Santosh Rawat
  • 61
  • 1
  • 12
  • 2
    As you already learned in your two other questions about this, you cannot save files to the server's local drive in a sandboxed plugin: https://stackoverflow.com/questions/39347386/how-to-resolve-dynamics-crm-plugin-system-security-permissions-fileiopermission – Henrik H Sep 12 '16 at 11:41
  • yeah i have already post the same problem in my previous questions but i cannot found a better approach to solve it? so if you have any solution than suggest me? – Santosh Rawat Sep 12 '16 at 11:52
  • What exactly do you imagine your code does? Where is it your expectation that the files you save end up? – Henrik H Sep 12 '16 at 11:53
  • and in my previous question i have not mentioned that whether it's online or on-premise? so i cannot get proper solution. – Santosh Rawat Sep 12 '16 at 11:53
  • i have also debug my code it works properly at filename its retrieve file name and at filepath it combined path also fileContent retrieved but when the File.WriteAllBytes created file its throws me to catch – Santosh Rawat Sep 12 '16 at 11:56

1 Answers1

3

I have bad news for you. When you develop plugins for CRM Online it will not have an access to server resources (like file system in your case) so your code will never work in CRM Online environment.

Andrew Butenko
  • 5,048
  • 1
  • 14
  • 13