Saving as and location predetermined without using sharepoint - InfoPath Dev
in

InfoPath Dev

Having trouble finding a blog or post that answers your question? Check out our Custom Search Page

Saving as and location predetermined without using sharepoint

Last post 09-01-2016 02:58 PM by AndreaP. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 08-26-2016 09:54 AM

    Saving as and location predetermined without using sharepoint

    Hi all! I'm new with InfoPath and I'm not a programmer or anything so I basically spend hours on forums while doing my InfoPath file... I'm also French, all that means that, I need easy English answers and step-by-step instructions with coding because I'm learning.

    I'm using InfoPath 2010 (and I finally managed to install Visual Studio tools for apps, so I have access to the code).

    I'm not using SharePoint since I don't have an online server, so I ask the users to save the file in a specific location (we have a shared repertory). Sorry if I'm saying things that are obvious or if I'm missing something, like I said, I'm a newbie!! 

    I would like for the file to be automatically named after a field where they will enter a date (not the actual date but the event date which can be 'now' but could also be on another day) followed by a number of the event since there will probably be many entries on the same day. It would look like '2016-08-26-001.xsn'. Also, I want the save location to be predetermined.

    I found many C# codes that I tried putting in my coding but I always have the message :

    'InfoPath cannot save the form. The form definiton file (.xsf) specifies that the saving functionality must be defined in the code, but the code does not define any event manager for OnSaveRequest.'

    My InfoPath is in French... I translated the error message because I couldn't find an english version of the error message online, I hope you will understand!

     

    These are the codes I tried:

           1st code -

    [InfoPathEventHandler(MatchPath="/my:myFields/my:Field1", EventType=InfoPathEventType.OnAfterChange)]

    public void Field1_OnAfterChange(DataDOMEvent e) {

     

    if(e.Operation == "Insert") {

    string saveFile = thisXDocument.DOM.selectSingleNode("my:myFields/my:Field1").text;

    thisXDocument.UI.SetSaveAsDialogFileName(saveFile + ".xml");

    }}

     

         2nd code -

    {

    string RequisitionNumber = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:Header/my:RequsitionNumber", NamespaceManager).InnerXml;if (this.New || e.IsSaveAs == false)

    {

    if (e.IsSaveAs)

    {

     

    this.SetSaveAsDialogLocation(@"\\ccplm\sw6\req\");

    this.SetSaveAsDialogFilename(RequisitionNumber);

    }

    e.CancelableArgs.Cancel =
    false;

    e.PerformSaveOperation();

    }

    else

    {

    e.CancelableArgs.Message =
    "This functionality has been disabled.";e.CancelableArgs.MessageDetails = "Only new forms can use the 'Save As' functionality.";

    }

     

    I tried each in these two ways:

    1st: going in the form options > save with custom code > modify > then Visual Studio opens and I put the code where it's written ''Type here the code to run before a 'save' or 'save as' operation''.

    2nd: going in the code directly and putting the code between the { } that are after 'public void InternalStartup ()'.

     

    I'm sorry I really tried looking for posts about this and I found many of them but the thing is that I don't know where exactly to put the code and maybe there's something I didn't do before that I don't know... 

    I did activate the C# coding!

    I hope I have said everything you need to help me...

    Thank you in advance!! :)

  • 08-26-2016 01:51 PM In reply to

    Re: Saving as and location predetermined without using sharepoint

     Hi - first, this is the right approach:

     1st: going in the form options > save with custom code > modify > then Visual Studio opens and I put the code where it's written ''Type here the code to run before a 'save' or 'save as' operation''.

    Your samples look like they are from older versions of IP. Here is some code that may get you on the right track - bolded items may need to be modified, and you may not want to set the dialog location, but I included it for completeness.

             public void FormEvents_Save(object sender, SaveEventArgs e)
            {
                // Write your code that will run before a Save or Save As operation here.
                string fileName = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field1", NamespaceManager).Value;

                this.SetSaveAsDialogLocation("D:\\Documents\\Temp");

                this.SetSaveAsDialogFilename(fileName + ".xml");

                // The Dirty property will be set to false if save is successful.
                e.PerformSaveOperation();

                // Write your code that will run after saving here.
                e.CancelableArgs.Cancel = false;
            }

    Hilary Stoupa

  • 08-29-2016 05:46 AM In reply to

    Re: Saving as and location predetermined without using sharepoint

     Thank you so much for answering Hilary!

     I tried what you said but I still get an error message... I asks me to define a manager of events for the OnSaveRequest, which I don't know what that is.

    That's what my code looks like now:

    using Microsoft.Office.InfoPath;
    using System;
    using System.Windows.Forms;
    using System.Xml;
    using System.Xml.XPath;
    using mshtml;

    namespace Formulaire3
    {
        public partial class FormCode
        {
            // Les variables membres ne sont pas prises en charge dans les formulaires activés pour le navigateur.
            // À la place, écrire et lire ces valeurs à partir de FormState
            // dictionnaire utilisant du code tel que le suivant :
            //
            // private object _memberVariable
            // {
            //     get
            //     {
            //         return FormState["_memberVariable"];
            //     }
            //     set
            //     {
            //         FormState["_memberVariable"] = value;
            //     }
            // }

            // REMARQUE : la procédure suivante est requise par Microsoft InfoPath.
            // Elle peut être modifiée à l’aide de Microsoft InfoPath.
            public void InternalStartup()
            {
                EventManager.FormEvents.Save += new SaveEventHandler(FormEvents_Save);
            }

            public void FormEvents_Save(object sender, SaveEventArgs e)
            {
                // Write your code that will run before a Save or Save As operation here.
                string fileName = MainDataSource.CreateNavigator().SelectSingleNode("/my:mesChamps/my:champ3", NamespaceManager).Value;

                this.SetSaveAsDialogLocation("C:\Users\portillaa\Documents\");

                this.SetSaveAsDialogFilename(fileName + ".xml");

                // The Dirty property will be set to false if save is successful.
                e.PerformSaveOperation();

                // Write your code that will run after saving here.
                e.CancelableArgs.Cancel = false;
            }
        }
    }
     

     Andrea

  • 08-29-2016 11:04 AM In reply to

    Re: Saving as and location predetermined without using sharepoint

     Ok so the set location works!!

    The set as filename too but now I have a different problem.

    I managed to make the code refer to a date field I have on the form (which is not actual) but I can't edit the date and time format that it will create on the dialog box...

    I can edit the format for the view on the form (YYYY-MM-DD)  but when the dialog box opens, it's written like that '2016-08-292016-08-29T14:00:54.xml' which I do noooot like! I want it to be like this: '2016-08-29-14-00.xml'

     

    my code:

     

    using Microsoft.Office.InfoPath;
    using System;
    using System.Windows.Forms;
    using System.Xml;
    using System.Xml.XPath;
    using mshtml;

    namespace Formulaire3
    {
        public partial class FormCode
        {
            public void InternalStartup()
            {
                EventManager.FormEvents.Save += new SaveEventHandler(FormEvents_Save);
            }

            public void FormEvents_Save(object sender, SaveEventArgs e)
            {
                // Write your code that will run before a Save or Save As operation here.
                string fileName = MainDataSource.CreateNavigator().SelectSingleNode("/my:mesChamps/my:groupe1/my:_champ1", NamespaceManager).Value;

                string hour = MainDataSource.CreateNavigator().SelectSingleNode("/my:mesChamps/my:groupe1/my:_champ8", NamespaceManager).Value;
                
                this.SetSaveAsDialogLocation("z:\\STL_COMMUN\\TVE\\TVE_2016");

                this.SetSaveAsDialogFilename(fileName + hour + ".xml");


                // The Dirty property will be set to false if save is successful.
                e.PerformSaveOperation();

                // Write your code that will run after saving here.
                e.CancelableArgs.Cancel = false;
            }
        }
    }

  • 08-29-2016 04:22 PM In reply to

    Re: Saving as and location predetermined without using sharepoint

    You can parse the value to a date time and then you can use ToString to format. Sample below.

    DateTime dateValue = new DateTime();

    string dateField = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field2", NamespaceManager).Value;

    DateTime.TryParse(dateField, out dateValue);

    string prettyDateTime = dateValue.ToString("yyyy-MM-dd-HH-mm");

    Hilary Stoupa

  • 08-31-2016 06:18 AM In reply to

    Re: Saving as and location predetermined without using sharepoint

     Thank you so much for your help!! I found another way around by asking the user to choose the date in scrollbars!

     Now this:

    I have mandatory fields but I also have many 'views' (is that how it's called in the English app? maybe it's window or page? In French it's view). The first view asks the user to to press a button for the type of form he/she will fill. All the forms are in the same InfoPath file, in different views. 

    In each forms, there are mandatory fields but I don't want to use the 'this cannot be virgin' option because if they are filling another form, it will still be asked to be filled.

    1st question: is there a way that IF a view is filled THEN some others don't have to be? Therefore there mandatory fields are not anymore?

    2nd question: I tried to do something on my own to resolve this, here's what I did: in the form 'A', there are 5 mandatory fields out of aprox 30 fields. In those 5 mandatory, I know that at least one (let's say 'x') is 100% sure to be filled, the 4 others (y1, y2, y3, y4) are sometimes omitted (it used to be a paper form, and they often don't fill one or many of those 4 fields).

    What I did is that I created formatting rules where the background color goes red.

    Field y1

    The rule applies if:

    y1 is empty AND
    x is not empty OR
    y2 is not empty OR
    y3 is not empty OR
    y4 is not empty

    The rule works. When y1 is empty and x is not empty, it turns red. Where the issue is is that even if I fill it, it goes red! Why why why? I tried everything, it fails when all the other are filled, when all the others are not filled and when some are.

    What am I doing wrong...

  • 08-31-2016 08:19 AM In reply to

    Re: Saving as and location predetermined without using sharepoint

    For your required fields that are conditionally required, you can add validation rules - like, on field1, the condition could be: If field1 is blank AND field2="x", make field1 required. I think in your formatting rule you may need to use some parenthesis to get everything squared away. You can set the first dropdown in the condition builder to "the expression" to see the XPath being used, then can create your own expressions, so you could have something like: (my:Y2 = "" or my:Y3 = "" or my:Y4 = "") and my:X = ""
    Hilary Stoupa

  • 08-31-2016 08:29 AM In reply to

    Re: Saving as and location predetermined without using sharepoint

     The thing is that I used buttons to lead to different views... And there's not 'if clicked' rule, which would be perfect!

    I don't understand what you said about the expression for the dropdown

  • 08-31-2016 08:31 AM In reply to

    Re: Saving as and location predetermined without using sharepoint

    Add a field to the main data source that isn't displayed on the form. When the user selects a view, set that field to the selection. Then you can use that for your validation rules.
    Hilary Stoupa

  • 09-01-2016 02:58 PM In reply to

    Re: Saving as and location predetermined without using sharepoint

     Thank you!!

     I have to go back to the first question. The code works, but the computers where the form is opened don't allow internet forms that have managed code. I have to find a way to set the location saving and the file name... Is it impossible because of those settings the computers have? I would have to ask an admin, which is possible but I don't know if they are able to let only one form have coding... This rule is basically for security (it's for my job).

    That's the computer rules I'm talking about (found on Microsoft website https://technet.microsoft.com/en-us/library/ee732407%28v=ws.10%29.aspx):

    Configure UAC Group Policy Settings

    Updated: October 29, 2009

    Applies To: Windows 7, Windows Server 2008 R2

    You must be logged on as a member of the local Administrators group to configure the User Account Control (UAC) Group Policy settings. You can also perform the procedure as a standard user if you are able to provide valid credentials for an administrator account at the User Account Control credential prompt.

    For information about UAC Group Policy settings, see Define the UAC Group Policy Settings in the planning section of this guide.

    To configure UAC Group Policy settings

        1. Click Start, type secpol.msc in the Search programs and files box, and then press ENTER.

        2. If the User Account Control dialog box appears, confirm that the action it displays is what you want, and then click Yes.

        3. In the console tree, double-click Local Policies, and then click Security Options.

        4. In the details pane, right-click the relevant UAC setting, and then click Properties.

        5. Make the desired changes, and then click OK.

     

Page 1 of 1 (10 items)
Copyright © 2003-2017 Qdabra Software. All rights reserved.
View our Terms of Use.