Using code to save locally with "domain" as trust level - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Using code to save locally with "domain" as trust level

Last post 01-05-2021 07:42 AM by steven. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 12-23-2020 02:29 AM

    • steven
    • Not Ranked
    • Joined on 12-22-2020
    • Posts 3

    Using code to save locally with "domain" as trust level

    I am making a form that will be used by people on the exact same computer and then it will be saved locally. It is to store data for delivery guys, so they fill in some information on a form and then head out. Now at night, all these forms will be processed by another script (using Robotic Process Automation).

    So I needed to make a button that would save the form locally with a certain unique filename and would then close the form. This all works fine on my local machine, with a "full trust" security level and using the following code:

    public void CTRL1_5_Clicked(object sender, ClickedEventArgs e)
            {
                string projectTitle = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:ID", this.NamespaceManager).Value; if (projectTitle == null || projectTitle == "")
                {
                    projectTitle = "IncorrectIdentifier";
                }
                string currentDirect = Directory.GetCurrentDirectory(); 
                this.SaveAs(currentDirect + "/" + projectTitle + ".XML");
                //the line below also closes the application. If one need to just close the form, use this.Close() instead.
                //System.Environment.Exit(0);  
            }  

    Now the problem arises when I move this form into my work environment. By the system settings it will not allow a form with "Full trust". So I thought to change the trust level to "domain", but then it seems that all the "System.IO" calls (such as "Directory.GetCurrentDirectory()" or more importantly "this.SaveAs" and "System.Environment.Exit(0)" ) will not work.

    I do not think I can reach my system administrator to allow for full trust forms. So I really wonder whether it is possible to save a file within a domain trust setting. The file will be stored locally, in the same map as the form is openend, so the user account from which the form is accessed will have rights to read and write within the folder. But I still get the following error:

        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.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
           at System.Security.CodeAccessPermission.Demand()
           at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
           at System.IO.Directory.InternalGetCurrentDirectory(Boolean checkHost)
           at System.IO.Directory.GetCurrentDirectory()
           at coded.FormCode.CTRL1_5_Clicked(Object sender, ClickedEventArgs e)
           at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
           at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

    This is pointed to the "GetCurrentDirectory" command, but if I change that it will point to the "SaveAs" command and if I change that it will point to the "Environment.Exit" command. And I basically need all three.

    Any help would be much appreciated.
    Filed under: , , ,
  • 12-23-2020 02:15 PM In reply to

    Re: Using code to save locally with "domain" as trust level

    So - I think you'll end up needing to be full trust here, but I'm wondering about your comment that you are moving this form. If you publish it instead, and open from the published location, is Full Trust ok?
    Hilary Stoupa

  • 12-23-2020 11:57 PM In reply to

    • steven
    • Not Ranked
    • Joined on 12-22-2020
    • Posts 3

    Re: Using code to save locally with "domain" as trust level

    Thansk for your reply. The problem is, we will be using it on a company intranet. This environment does not support Visual Studio, so I can not program inside the intranet. That means I am doing the coding on my personal laptop, and then move the file to the company's intranet. This way the code is still intact, but I do need the full trust, which the company does not allow for.

    So you reckon it is impossible to use the code as I did with a Domain trust level?

  • 01-04-2021 11:43 AM In reply to

    Re: Using code to save locally with "domain" as trust level

     As per the documentation, this will require full trust:

    https://docs.microsoft.com/en-us/previous-versions/office/infopath-form/aa947229(v=office.15)#remarks

    This member can be accessed only by forms opened from a form template that has been configured to run with full trust using the Security and Trust category of the Form Options dialog box. This member requires full trust for the immediate caller and cannot be used by partially trusted code. 

    Hilary Stoupa

  • 01-05-2021 07:42 AM In reply to

    • steven
    • Not Ranked
    • Joined on 12-22-2020
    • Posts 3

    Re: Using code to save locally with "domain" as trust level

    Thanks Hilary, this is quite clear. So I guess my method can not be implemented.
Page 1 of 1 (5 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.