Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Getting System.Security.Permissions.FileIOPermission error while working with Plug-In in Online Dynamics CRM 2016

$
0
0

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());
           }

Viewing all articles
Browse latest Browse all 46379

Trending Articles