Monday, December 12, 2011

Saving an InfoPath Form

The problem:
The requirements state that the browser-based form I designed should first be saved in the forms library as an empty form (this is done by some code), the permission inheritance for the corresponding list item should be broken, and then the Contribute permissions should be granted to some particular user (this is also done by some code.) Thus, this user will only be able to see and fill out his or her particular forms. Any time the user submits the form, the form should be submitted to its form library and its file name should not change (otherwise a new entry will appear in the Forms library and it will not have the same permissions as the original entry... which is not so great – remember, each form should only be visible to one particular user!)
While configuring the submit options for the form, I created a new Submit data connection. However, the Data Connection Wizard asks for the file name with which the form will be submitted. How to populate that field? It comes from the SharePoint list, but there is no way to query the SharePoint list from the Data Connection Wizard.
The solution:
1)      Create the new field for the file name (give it a name, say, strFileName).

2)       In the Form Options, make sure that the Save As button is NOT displayed. Since it is the code and not the user who creates the original empty form, the Save button as well as the Close buttons should not be displayed, either.

3)      While configuring Submit Options, specify this field as the file name with which the form will be submitted. Also make sure that the Overwrite checkbox is checked.

4)      Now we need to think of how to populate that particular field. It has to be populated when the form is loading. Thus, we need to add some code to the form loading event handler. What kind of code?

       To answer this question, look at the URL for the request when the form is displayed in the browser. The  file name can be found in the query string, in the XmlLocation parameter. (If the form does not have a file name yet, the XmlLocation parameter is missing.) Thus, the code we need to write is something like this:

public void FormEvents_Loading(object sender, LoadingEventArgs e)
      {
            // Write your code here.
            string xmlLocation = String.Empty;
            e.InputParameters.TryGetValue("XmlLocation"out xmlLocation);
            if (!String.IsNullOrEmpty(xmlLocation))
            {
                string[] parts = xmlLocation.Split(new char[] { '/' });
                string fileName = parts[parts.Length - 1];

                XPathNavigator form = MainDataSource.CreateNavigator();

form.SelectSingleNode("/my:myFields/my:strFilename", NamespaceManager).SetValue(fileName);
            }
 }

When the form is displayed, nothing is done if the file name for the list item is now known yet, because the XmlLocation parameter is missing. But when the file name is known for the list item, the XmlLocation parameter is parsed to find the file name. Then, the new strFileName field is populated. When the form is submitted, the old file that has this file name is overwritten. Voila! 

No comments:

Post a Comment