How can i submit to a Document (or Forms) library with C# code? - InfoPath Dev

InfoPath Dev

Use our Google Custom Search for best site search results.

How can i submit to a Document (or Forms) library with C# code?

Last post 03-10-2009 11:28 AM by buck_murdock. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 03-09-2009 02:26 PM

    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"];



    But i get an error which says that the Site might be offline, read only or unavailable, but it's not.

    InfoPath cannot submit the form.
    An error occurred while the form was being submitted.

    The form cannot be submitted to the following location: - 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)




    // 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);




    // 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)


    "/my:myFields/my:RequestorEmail", ns)


    "/my:myFields/my:RequestorOffice", ns)


    "/my:myFields/my:RequestorMailstop", ns)


    "my:myFields/my:RequestorExtension", ns)


    "/my:myFields/my:RequestorDepartment", ns)


    // Clean up.

    xnMyForm = null;


    result =




    catch (Exception ex)


    MessageBox.Show("The following error occurred: " +






    public void SubmitButton_Clicked(object sender, ClickedEventArgs e)


    // Submit the Full Form to the Software Quotes Document Library

    FileSubmitConnection docLib = (FileSubmitConnection)this.DataConnections["DocumentLibrarySubmit"];



    // Delete all Method nodes from the CAML Batch XML

    XPathNavigator batch = DataSources["CustomListCAML"].CreateNavigator();

    XPathNodeIterator iter = batch.Select("/Batch/Method");

    int methodNodesCount = iter.Count;

    XPathNavigator firstMethodNav =



    XPathNavigator lastMethodNav =batch.SelectSingleNode("/Batch/Method[" + methodNodesCount.ToString() + "]",




    // Retrieve the rows of the repeating table

    XPathNavigator root = MainDataSource.CreateNavigator();

    XPathNodeIterator rows = root.Select(

    "/my:myFields/my:rows/my:row", NamespaceManager);

    // Retrieve the values for the custom list item

    string title = root.SelectSingleNode("my:myFields/my:title",


    //Retrieve the RequestorFullName of the form

    string RequestorFullName = root.SelectSingleNode("my:myFields/my:RequestorFullName",


    //Retrieve the RequestorDepartment of the form

    string RequestorDepartment = root.SelectSingleNode("my:myFields/my:RequestorDepartment",


    //Retrieve the RequestorEmail of the form

    string RequestorEmail = root.SelectSingleNode("my:myFields/my:RequestorEmail",


    //Retrieve the AccountNumber of the form

    string AccountNumber = root.SelectSingleNode("my:myFields/my:ChargeWorkTo",


    //Retrieve the RequestorExtension of the form

    string RequestorExtension = root.SelectSingleNode("my:myFields/my:RequestorExtension",


    //Retrieve the OfficeLocation of the form

    string OfficeLocation = root.SelectSingleNode("my:myFields/my:RequestorOffice",


    //Retrieve the MailStop of the form

    string MailStop = root.SelectSingleNode("my:myFields/my:RequestorMailstop",



    // Loop through the rows of the repeating table

    // and construct the CAML Batch XML

    int counter = 1;while (rows.MoveNext())


    // Retrieve the SoftwareTitle

    string SoftwareTitle = rows.Current.SelectSingleNode(

    "my:SoftwareTitle", NamespaceManager).Value;

    // Retrieve the SoftwarePrice

    string SoftwarePrice = rows.Current.SelectSingleNode(

    "my:SoftwarePrice", NamespaceManager).Value;

    // Retrieve the LicensedPC

    string 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




    // Submit the Title of the request



    // Submit the rows to the Lists web service to update the custom list




    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)


    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);


    XmlDocument methodXML = new XmlDocument();


    XPathNavigator batch = DataSources["CustomListCAML"].CreateNavigator();XPathNavigator batchNav = batch.SelectSingleNode("/Batch", NamespaceManager);





  • 03-10-2009 11:28 AM In reply to

    Re: How can i submit to a Document (or Forms) library with C# code?

    I did find this article which sounds like it would be perfect, because then i wouldnt need our purchaser to go to a seperate library to look up the actual form and get the attachment.  But i have no item.Update() line in my code and following the information from the article it references, i'm unable to even test the code because i dont have access to the SharePoint server to be able to use the Windows® SharePoint® Services reference when writing my code.

    But if there is another way I can accomplish the task similar to this that would be perfect.  Either extract the attachment from the form and add it as the list item attachment (or upload it to a document library and link to it from the list) or just wrap up the whole form and attach it to the list item (or again upload it to a document library and link to it from the list).

Page 1 of 1 (2 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.