-
buck_murdock
- Joined on 01-26-2009
- Posts 21
|
How can i submit to a Document (or Forms) library with C# code?
I'm working on a form, and I currently have some C# code in it which uses the UpdateListItem webservice to submit the contents of the InfoPath Form to a SharePoint list. My form will also have a file attachment control on it, and try as i might i cannot get the file attachment code right to upload the file to where I want it, so i'm resorting back to plan B.
I'd like to also submit this form to a Forms Library (or Document Library), so when the form is submitted the information goes to the Sharepoint List like it does now, but also goes to a Library so if the person needs to see what quote that was attached using the File Attachment Control, they can go to the Document Library and open the actual form and get to the attached file.
I tried using this piece of Code:
// Submit the Full Form to the Software Quotes Document Library FileSubmitConnection docLib = (FileSubmitConnection)this.DataConnections["DocumentLibrarySubmit"];
docLib.Execute();
But i get an error which says that the Site might be offline, read only or unavailable, but it's not.
System.Net.WebException InfoPath cannot submit the form. An error occurred while the form was being submitted.
The form cannot be submitted to the following location: https://slacspace.slac.stanford.edu/sites/support/Purchasing/SoftwareRequestQuotes/Forms/AllItems.aspx/2009-03-09T14_21_43 - Adams_ Tyler S..xml The site may be offline, read-only, or otherwise unavailable. Unspecified error
at Microsoft.Office.InfoPath.Internal.MomExceptionHelper.ExecuteDataConnectionAction(OMCall d) at Microsoft.Office.InfoPath.Internal.FileSubmitConnectionHost.Execute() at Software_Request_Form_Rev2.FormCode.SubmitButton_Clicked(Object sender, ClickedEventArgs e) at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent) at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)
Here's my entire code snippit incase there's something lingering somewhere else and causing interference:
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using System.DirectoryServices;
using System.Web;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.InfoPath.SemiTrust;namespace Software_Request_Form_Rev2
{
public partial class FormCode
{
// Member variables are not supported in browser-enabled forms.
// Instead, write and read these values from the FormState
// dictionary using code such as the following:
//
// private object _memberVariable
// {
// get
// {
// return FormState["_memberVariable"];
// }
// set
// {
// FormState["_memberVariable"] = value;
// }
// }
// NOTE: The following procedure is required by Microsoft Office InfoPath.// It can be modified using Microsoft Office InfoPath.
public void InternalStartup()
{ ((ButtonEvent)EventManager.ControlEvents["SubmitButton"]).Clicked += new ClickedEventHandler(SubmitButton_Clicked);EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
} public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
try
{
// Get the user name of the current user.string userName = this.Application.User.UserName;
// Create a DirectorySearcher object using the user name
// as the LDAP search filter. If using a directory other
// than Exchange, use sAMAccountName instead of mailNickname.DirectorySearcher searcher = new DirectorySearcher(
"(mailNickname=" + userName + ")");
// Search for the specified user.SearchResult result = searcher.FindOne();
// Make sure the user was found.if (result == null)
{ MessageBox.Show("Error finding user: " + userName);
}
else
{
// Create a DirectoryEntry object to retrieve the collection
// of attributes (properties) for the user.DirectoryEntry employee = result.GetDirectoryEntry();
// Assign the specified properties to string variables.string FullName = (employee.Properties["displayName"].Value != null) ? employee.Properties["displayName"].Value.ToString() : "";
string Mail = (employee.Properties["mail"].Value != null) ? employee.Properties["mail"].Value.ToString() : "";string Location = (employee.Properties["physicalDeliveryOfficeName"].Value != null) ? employee.Properties["physicalDeliveryOfficeName"].Value.ToString() : "";
//Retrieve the Street Address from Active Directory and delimit to show only the mailstop number.string Address = (employee.Properties["streetAddress"].Value != null) ? employee.Properties["streetAddress"].Value.ToString() : "";
string searchKey = "MS";int searchKeyIndex = Address.IndexOf(searchKey);
string MailStop = Address.Substring(searchKeyIndex + searchKey.Length).Trim();string Phone = (employee.Properties["telephoneNumber"].Value != null) ? employee.Properties["telephoneNumber"].Value.ToString() : "";
string Department = (employee.Properties["department"].Value != null) ? employee.Properties["department"].Value.ToString() : "";
// Create an XPathNavigator to walk the main data source
// of the form.XPathNavigator xnMyForm = this.CreateNavigator();
XmlNamespaceManager ns = this.NamespaceManager;
// Set the fields in the form.xnMyForm.SelectSingleNode("/my:myFields/my:RequestorFullName", ns)
.SetValue(FullName); xnMyForm.SelectSingleNode("/my:myFields/my:RequestorEmail", ns)
.SetValue(Mail); xnMyForm.SelectSingleNode("/my:myFields/my:RequestorOffice", ns)
.SetValue(Location); xnMyForm.SelectSingleNode("/my:myFields/my:RequestorMailstop", ns)
.SetValue(MailStop); xnMyForm.SelectSingleNode("my:myFields/my:RequestorExtension", ns)
.SetValue(Phone); xnMyForm.SelectSingleNode("/my:myFields/my:RequestorDepartment", ns)
.SetValue(Department);
// Clean up.xnMyForm = null;
searcher.Dispose(); result = null;
employee.Close();
}
} catch (Exception ex)
{ MessageBox.Show("The following error occurred: " +
ex.Message.ToString()); throw;
}
} public void SubmitButton_Clicked(object sender, ClickedEventArgs e)
{
// Submit the Full Form to the Software Quotes Document LibraryFileSubmitConnection docLib = (FileSubmitConnection)this.DataConnections["DocumentLibrarySubmit"];
docLib.Execute();
// Delete all Method nodes from the CAML Batch XMLXPathNavigator batch = DataSources["CustomListCAML"].CreateNavigator();
XPathNodeIterator iter = batch.Select("/Batch/Method");int methodNodesCount = iter.Count;
XPathNavigator firstMethodNav =batch.SelectSingleNode("/Batch/Method[1]",
NamespaceManager); XPathNavigator lastMethodNav =batch.SelectSingleNode("/Batch/Method[" + methodNodesCount.ToString() + "]",
NamespaceManager);
firstMethodNav.DeleteRange(lastMethodNav);
// Retrieve the rows of the repeating tableXPathNavigator root = MainDataSource.CreateNavigator();
XPathNodeIterator rows = root.Select("/my:myFields/my:rows/my:row", NamespaceManager);
// Retrieve the values for the custom list itemstring title = root.SelectSingleNode("my:myFields/my:title",
NamespaceManager).Value;
//Retrieve the RequestorFullName of the formstring RequestorFullName = root.SelectSingleNode("my:myFields/my:RequestorFullName",
NamespaceManager).Value;
//Retrieve the RequestorDepartment of the formstring RequestorDepartment = root.SelectSingleNode("my:myFields/my:RequestorDepartment",
NamespaceManager).Value;
//Retrieve the RequestorEmail of the formstring RequestorEmail = root.SelectSingleNode("my:myFields/my:RequestorEmail",
NamespaceManager).Value;
//Retrieve the AccountNumber of the formstring AccountNumber = root.SelectSingleNode("my:myFields/my:ChargeWorkTo",
NamespaceManager).Value;
//Retrieve the RequestorExtension of the formstring RequestorExtension = root.SelectSingleNode("my:myFields/my:RequestorExtension",
NamespaceManager).Value;
//Retrieve the OfficeLocation of the formstring OfficeLocation = root.SelectSingleNode("my:myFields/my:RequestorOffice",
NamespaceManager).Value;
//Retrieve the MailStop of the formstring MailStop = root.SelectSingleNode("my:myFields/my:RequestorMailstop",
NamespaceManager).Value;
// Loop through the rows of the repeating table
// and construct the CAML Batch XMLint counter = 1;while (rows.MoveNext())
{
// Retrieve the SoftwareTitlestring SoftwareTitle = rows.Current.SelectSingleNode(
"my:SoftwareTitle", NamespaceManager).Value;
// Retrieve the SoftwarePricestring SoftwarePrice = rows.Current.SelectSingleNode(
"my:SoftwarePrice", NamespaceManager).Value;
// Retrieve the LicensedPCstring LicensedPC = rows.Current.SelectSingleNode(
"my:LicensedPC", NamespaceManager).Value;
// Retrieve the LicensedUser
//string LicensedUser = rows.Current.SelectSingleNode(
//"my:LicensedUser", NamespaceManager).Value;
// Add an item to the CAML Batch XML
AddMethodNode(counter, title, SoftwareTitle, SoftwarePrice, RequestorFullName, RequestorDepartment, RequestorEmail, AccountNumber, RequestorExtension, OfficeLocation, MailStop, LicensedPC);
// Increment the counter
counter++;
}
// Submit the Title of the requestbatch.SelectSingleNode("/Batch/Method/Field[@Name='Title']",
NamespaceManager).SetValue(title);
// Submit the rows to the Lists web service to update the custom listDataConnections["CustomListItemsSubmit"].Execute();
} private void AddMethodNode(int id, string title, string SoftwareTitle, string SoftwarePrice, string RequestorFullName, string RequestorDepartment, string RequestorEmail, string AccountNumber, string RequestorExtension, string OfficeLocation, string MailStop, string LicensedPC)
{ System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendFormat( "<Method ID=\"{0}\" Cmd=\"New\">", id.ToString());sb.AppendFormat("<Field Name=\"Title\">{0}</Field>", title);
sb.AppendFormat( "<Field Name=\"SoftwareTitle\">{0}</Field>", SoftwareTitle);sb.AppendFormat("<Field Name=\"SoftwarePrice\">{0}</Field>", SoftwarePrice);
sb.AppendFormat( "<Field Name=\"LicensedPC\">{0}</Field>", LicensedPC);
//sb.AppendFormat("<Field Name=\"LicensedUser\">{0}</Field>", LicensedUser);sb.AppendFormat("<Field Name=\"RequestorFullName\">{0}</Field>", RequestorFullName);
sb.AppendFormat( "<Field Name=\"RequestorDepartment\">{0}</Field>", RequestorDepartment);sb.AppendFormat("<Field Name=\"RequestorEmail\">{0}</Field>", RequestorEmail);
sb.AppendFormat( "<Field Name=\"AccountNumber\">{0}</Field>", AccountNumber);sb.AppendFormat("<Field Name=\"OfficeLocation\">{0}</Field>", OfficeLocation);
sb.AppendFormat( "<Field Name=\"MailStop\">{0}</Field>", MailStop);sb.AppendFormat("<Field Name=\"PhoneNumber\">{0}</Field>", RequestorExtension);
sb.AppendFormat( "</Method>");XmlDocument methodXML = new XmlDocument();
methodXML.LoadXml(sb.ToString()); XPathNavigator batch = DataSources["CustomListCAML"].CreateNavigator();XPathNavigator batchNav = batch.SelectSingleNode("/Batch", NamespaceManager);
batchNav.AppendChild(methodXML.DocumentElement.CreateNavigator());
}
}
}
|
|