We submit our browser enabled forms to a web service that uses code similar to what I've pasted below. Here's a brief description of the input parameters:
-
fileName = The name you want to file to have when it is saved to the library.
-
fileContents = The complete xml passed as a string
-
formName = What type of form is this? "resume", "contact us", "product order", etc... This is a hidden field in the infopath form which we set the default value at design time. Having this value helps us determine which library to save the form submission to.
Within the code we also access a few custom appSettings stored in the web.config to help us determine which site, web, and library to work from. The nice thing about this solution is that there is no managed code in the InfoPath form.
[WebMethod]
public void UploadFile(string fileName, string fileContents, string formName)
{
_fileName = fileName;
_fileContents = fileContents
_formName = formName;
SPSecurity.CodeToRunElevated codeToRunElevated = new SPSecurity.CodeToRunElevated(UploadFileUsingElevatedPrivileges);
SPSecurity.RunWithElevatedPrivileges(codeToRunElevated);
}
private void UploadFileUsingElevatedPrivileges()
{
using (SPSite spSite = new SPSite(ConfigurationManager.AppSettings["TargetURL"]))
{
string webName = ConfigurationManager.AppSettings["WebFor" + _formName];
using (SPWeb spWeb = spSite.OpenWeb(webName))
{
string libraryName = ConfigurationManager.AppSettings["LibraryFor" + _formName];
SPFileCollection fileCollection = spWeb.Folders[libraryName].Files;
System.Text.UTF8Encoding unicode = new System.Text.UTF8Encoding();
Byte[] encodedBytes = unicode.GetBytes(_fileContents);
fileCollection.Add(_fileName + ".xml", encodedBytes, true);
return;
}
}
}
}