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

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

    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 Library

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

    docLib.Execute();

     

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

    batch.SelectSingleNode("/Batch/Method[1]",

    NamespaceManager);

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

    NamespaceManager);

    firstMethodNav.DeleteRange(lastMethodNav);

     

    // 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",

    NamespaceManager).Value;

    //Retrieve the RequestorFullName of the form

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

    NamespaceManager).Value;

    //Retrieve the RequestorDepartment of the form

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

    NamespaceManager).Value;

    //Retrieve the RequestorEmail of the form

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

    NamespaceManager).Value;

    //Retrieve the AccountNumber of the form

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

    NamespaceManager).Value;

    //Retrieve the RequestorExtension of the form

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

    NamespaceManager).Value;

    //Retrieve the OfficeLocation of the form

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

    NamespaceManager).Value;

    //Retrieve the MailStop of the form

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

    NamespaceManager).Value;

     

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

    counter++;

    }

     

    // Submit the Title of the request

    batch.SelectSingleNode("/Batch/Method/Field[@Name='Title']",

    NamespaceManager).SetValue(title);

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

    DataConnections["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());

    }

    }

    }

  • 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 http://www.bizsupportonline.net/blog/2009/01/submit-infopath-form-sharepoint-list-attachment/ 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.