web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Dear Santosh,

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

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

  • Community Member Profile Picture
    on at

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

    Kind Regards,

    Santa

  • MMK Profile Picture
    745 on at

    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
    on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans