Tuesday, 21 February 2012

SharePoint: How to programmatically submit your form to document library?

How to programmatically submit your form to document library?
First create a new data connection in Infotpath 2010 as a Submit and To a Document Library on a SharePoint site. Don't't worry in entering any SharePoint Server's url and destination document library, because this values can be overridden in code. Once you have created the new Data Connection, name it as SharePoint Library Submit.
infopath2
Now add a simple button, and in the buttons propery windows select Action= Submit, press Submit Options and check Perform Custom action using  Code and finally press Edit Code.
infopath3
Add the following code in the FormEvents_Submit event
public void FormEvents_Submit(object sender, SubmitEventArgs e)
{
    try
    {
        FileSubmitConnection spConn = DataConnections["SharePoint Library Submit"] as FileSubmitConnection;
        spConn.FolderUrl = "http://yourServer/yourDestinationDocLibUrl/"l;
        spConn.Filename.SetStringValue("YourFileName.xml");
        spConn.Execute();
        e.CancelableArgs.Cancel = false;
    }
    catch (Exception ex)
    {
        e.CancelableArgs.Cancel = true;
    }
}
Reference: http://www.dev4side.com/community/technical-articles/sharepoint-2010/what-should-you-know-before-development-in-infopath-2010-.aspx