Thursday 12 September 2013

InfoPath: Submitting a form to 'this' folder within a document library

Commonly users want to create and submit a InfoPath form in the same location as they opened it regardless whether it is the root of a document library or within a folder or document set.

Obviously the save button is available, however they will have to navigate to the correct folder or there may be naming conventions which a submit command can satisfy.

Without code you can submit all forms to the same library using a SharePoint connection, in order to save to a specific library or folder you need to use custom code. Within a form to be used in form filler you can simply use this.save or this.saveas, however within Form Services (web forms) Form the Microsoft.Office.InfoPath namespace / binary is only a sub set of the form filler binary, and neither save or saveas are methods in this namespace.

The below code works for both new and opened forms. In order to work you need to create a SharePoint submit datasource within the InfoPath form (in this case called submit).

using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;

namespace BrowserTest
{
public partial class FormCode
{
//Is an object used when the form is opened to store the Url the form was openned from
private object _uri
{
get
{
return FormState["_uri"];
}
set
{
FormState[
"_uri"] = value;
}
}

public void InternalStartup()
{
EventManager.FormEvents.Loading +=
new LoadingEventHandler(FormEvents_Loading);
EventManager.FormEvents.Submit +=
new SubmitEventHandler(FormEvents_Submit);
}

public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
try
{
//Input parameters are query string variables
//Save location is the variable name when the form is first openned
this._uri = e.InputParameters["SaveLocation"].ToString();
}
catch
{
//If the save location is not present the exception will be caught
//The XMLLocation will be used instead
string XMLLoc = e.InputParameters["XmlLocation"].ToString();
//The XMLLocation is relative so there the site collection url is appended to it
this._uri = this.ServerInfo.SharePointServerRootUrl + XMLLoc.Substring(1, XMLLoc.LastIndexOf("/"));
}
}

public void FormEvents_Submit(object sender, SubmitEventArgs e)
{
//When the form is submitted
try
{
//Get the SharePoint submit connection (as previous described)
FileSubmitConnection dc = (FileSubmitConnection)this.DataConnections["Submit"];
if (dc != null)
{
//Sets the folder url of the submit method then executes
dc.FolderUrl = this._uri;
dc.Execute();
e.CancelableArgs.Cancel =
false;
}
}
catch
{
//Cancels the submit if an exception is thrown
e.CancelableArgs.Cancel = true;
}

}
}
}







This code can be added to a form which is published as an administrator approved form (with full control) and then published through form services.

No comments:

Post a Comment