Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting System.Security.Permissions.FileIOPermission error while working with Plug-In in Online Dynamics CRM 2016

    hi santosh,

    i have same requirement . i have to download the attachments from notes entity .am getting the same error. can you tell me how you downloaded the attachments from notes entity.

    Thanks Regards

    karthik

  • MMK Profile Picture
    MMK 745 on at
    RE: Getting System.Security.Permissions.FileIOPermission error while working with Plug-In in Online Dynamics CRM 2016

    Hi Santosh ,

    I want to use a third SmartyStreet Dll to fetch address it uses memorystream to post and fetch data from their exterrnal APIs

    causing the same error any work around

    It has a solution using javascript but

    Client dont want to use JS solution

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting System.Security.Permissions.FileIOPermission error while working with Plug-In in Online Dynamics CRM 2016

    Hi, Andrii thanks for your support, actually i have posted this issue earlier but finally i have achieved my goal

    Kind Regards,

    Santa

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Getting System.Security.Permissions.FileIOPermission error while working with Plug-In in Online Dynamics CRM 2016

    Dear Santosh,

    Please refer my answer in SO - stackoverflow.com/.../39449596

    I'm afraid you will not get other answer here.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans