InfoPath Save Button

_IPLogo

We had the issue that often users “saved” the browser-based InfoPath form into a workflow activated forms library instead of hitting the “Submit” button. The default InfoPath save function could not be turned off altogether to allow them to later save required changes to the form. Since we used functions in the form that got activated once the form was submitted (MemorizeSubmission()), the form was rendered useless and had to be redone.  

It is a while back I did this and I have trouble to recall every single step, but I hope you get the drift. 

There is a line of code that lets you pull the path to the forms library were the form is getting saved into. This code only works at the initial load of the form. Therefore I transferred the path into a InfoPath textfield (savelocation) for later use. The workflow would rename the forms filename to have a unique tracking name based on the workflow function. Once the form was submitted this filename is pushed back into the form to show its name in the title section. With the path to the forms library and the filename (filename + “.xml”) I was able to recreate the path to the location with the name of the form. The trick is to create a customized button in the InfoPath form with the location to the forms library and to select “overwrite” the existing form. The Save_Clicked() method would then override the path to the forms library in that button and replace it with what is saved in the InfoPath textfield. 

Since the form is only basically copied back to the forms library and never really saved as a save function a message appears in the form when you close it “if you are sure to close, changes to the form might be lost” – but our users can live with this message. As long as they hit the save button, the form is updated on the server.  

Create a “Save” button on the InfoPath form and define a “rule” to make a Data Connection to any Document/Forms Library and make sure to select “Allow overwrite if file exists”. The path to the library will be overwritten in the code. The filename points to the textfield that contains the filename.

You can turn the default InfoPath toolbar save button(s) off in the settings. I set the custom save button to hide as long as the submit button was shown on the form. Once the form was submitted the submit button would disappear and the custom save button would take over.

using System;
using System.Xml;
using System.Xml.XPath;
using Microsoft.Office.InfoPath;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;

namespace MyInfoPathForm
{
    public partial class FormCode
    {
        private object _URI
        {
            get { return FormState["_URI"]; }
            set { FormState["_URI"] = value; }
        }

        private object _Location
        {
            get { return FormState["_Location"]; }
            set { FormState["_Location"] = value; }
        }

        private object _Folder
        {
            get { return FormState["_Folder"]; }
            set { FormState["_Folder"] = value; }
        }

        // NOTE: The following procedure is required by Microsoft Office InfoPath.
        // It can be modified using Microsoft Office InfoPath.
        public void InternalStartup()
        {
            EventManager.FormEvents.Submit += new SubmitEventHandler(FormEvents_Submit);
            EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
            ((ButtonEvent)EventManager.ControlEvents["Save"]).Clicked += new ClickedEventHandler(Save_Clicked);
        }

public void FormEvents_Submit(object sender, SubmitEventArgs e)

        {

            //Get a reference to the submit data connection

            FileSubmitConnection fc = (FileSubmitConnection)DataConnections[“Submit”];

            //Modify the URL we want to submit to by concatenating the

            //Location and FolderName values

            fc.FolderUrl = _Location.ToString() + “/” + _Folder.ToString();

            XPathNavigator xPathNav = MainDataSource.CreateNavigator();

            XPathNavigator UrlNode = xPathNav.SelectSingleNode(“/my:form/my:savelocation”, NamespaceManager);

            UrlNode.SetValue(fc.FolderUrl);

            if (Errors.Count == 0)

            {

                MemorizeSubmission();

            }

            //Execute the submit connection

            try

            {

                fc.Execute();

                e.CancelableArgs.Cancel = false;

            }

            catch (Exception)

            {

                e.CancelableArgs.Cancel = true;

            }

        }

        public void Save_Clicked(object sender, ClickedEventArgs e)

        {

            //Get a reference to the submit data connection

            FileSubmitConnection fc2 = (FileSubmitConnection)DataConnections[“Save”];

            //Modify the URL we want to submit to by concatenating the

            //Location and FolderName values

            XPathNavigator xPathNav = MainDataSource.CreateNavigator();

            XPathNavigator Save = xPathNav.SelectSingleNode(“/my:form/my:savelocation”, NamespaceManager);

            string save = Save.Value;

            fc2.FolderUrl = save + “/”;

            //Execute the submit connection

            try

            {

                fc2.Execute();

            }

            catch (Exception ex)

            {

                throw (ex);

            }

        }

public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
            //Get the Uri (or SaveLocation in a browser form) of where 
            //the form was opened 
            //See if the form was opened in the browser
            Boolean OpenedInBrowser = Application.Environment.IsBrowser;

                        
            if (New)
            {
                //If so, we will get the "SaveLocation" from the InputParameters
                if (OpenedInBrowser)
                {
                    _URI = e.InputParameters["SaveLocation"].ToString();
                }
                else
                {
                    //If it was opened in the client, we will get the Uri
                    _URI = Template.Uri.ToString();
                }
               
                //Populate the fields on the form - keep in mind, this is not necessary - 
                //this is simply to see the process in action
                PopulateLibInfo(OpenedInBrowser);

                // get data from worksheets preparer 
                LoadWorksheetsPreparerData();

                //public string Options.DisableSave {set;}
            }
          
            // sets the filename 
            XPathNavigator xPathNav = MainDataSource.CreateNavigator();
            XPathNavigator Filename = xPathNav.SelectSingleNode("/my:form/my:formname", NamespaceManager);
            string filename = Filename.Value;

            XPathNavigator UrlNode = xPathNav.SelectSingleNode("/my:form/my:filename", NamespaceManager);
            UrlNode.SetValue(filename + ".xml");

        }


        private void PopulateLibInfo(Boolean OpenedInBrowser)
        {
            //Get the Uri stored in the FormState Dictionary variable
            string uri = _URI.ToString();

            //Create a variable to store the path (URL) to the document library
            string path = "";
            if (OpenedInBrowser == true)
            {
                //If we are open in the browser, the strUri value is just
                //the server name and library - so we just need to get the URL
                //without the last "/"
                path = uri.Substring(0, uri.LastIndexOf("/"));
            }
            else
            {
                //Parse just the path to the document library - 
                //this would return something like this: http://server/library                				
                path = uri.Substring(0, uri.IndexOf("Forms") - 1);
            }

            //Now, parse the URL to where the document library resides - 
            //this would return something like: http://server or http://server/site
            string location = path.Substring(0, path.LastIndexOf("/"));

            //Lastly, parse the URL to return just the document library name - 
            //in this case,we are looking for the last "/" character 
            // knowing that what comes after this is the document library name
            string folder = path.Substring(path.LastIndexOf("/") + 1);

            //store the fields  â we will use these values
            //in the Submit process
            _Location = location;
            _Folder = folder;

           }

       private void MemorizeSubmission()
        {
            XPathNavigator xPathNav = MainDataSource.CreateNavigator();
            XPathNavigator indicatorNode = xPathNav.SelectSingleNode("/my:form/my:submitted", NamespaceManager);
            indicatorNode.SetValue("1");
        }
    }
}

Based on Source: http://blogs.msdn.com/b/infopath/archive/2006/11/08/submitting-to-this-document-library.aspx

Is this Pages usefull? Usefull Useless 2 of 2 people say this Pages is usefull.