How to Add the Attachment from an InfoPath 2007 Form to a SharePoint 2007 (MOSS/WSS) Document Library



I have an InfoPath 2007 Form where I have a Text Box, a File Attachment field and a Button. I changed the property of the button to make it a Submit button. My target is to post the Form’s instance into a Form Library. And then put the file which I have attached through the File Attachment field to a separate Document Library.


In the Button Properties Pop-up, I have selected “Rules and Custom Code” as Action. Added a Rule to post the Form Instance in a SharePoint Form Library. I have used VSTA to add the necessary code to the Form Template. You can find it in Tools>Programming Menu. You can use VSTO also. I added a reference of “Windows SharePoint Services” and used the following code:


using Microsoft.Office.InfoPath;


using System;


using System.Xml;


using System.Xml.XPath;


using Microsoft.SharePoint;


using System.IO;


 


namespace Template1


{


    public partial class FormCode


    {


        public void InternalStartup()


        {


 


            ((ButtonEvent)EventManager.ControlEvents[“Submit”]).Clicked += new ClickedEventHandler(Submit_Clicked);


        }


 


        public void Submit_Clicked(object sender, ClickedEventArgs e)


        {


            XPathNavigator docXN = this.CreateNavigator();


            XPathNavigator opnXN = docXN.SelectSingleNode(“/my:myFields/my:fileup”, this.NamespaceManager);


            byte[] attachmentNodeBytes = Convert.FromBase64String(opnXN.ToString());


 


            // Position 20 contains a DWORD indicating the length of the


            // filename buffer. The filename is stored as Unicode so the


            // length is multiplied by 2.


            int fnLength = attachmentNodeBytes[20] * 2;


            byte[] fnBytes = new byte[fnLength];


 


            // The actual filename starts at position 24 . . .


            for (int i = 0; i < fnBytes.Length; i++)


            {


                fnBytes[i] = attachmentNodeBytes[24 + i];


            }


 


            // Convert the filename bytes to a string. The string


            // terminates with \0 so the actual filename is the


            // original filename minus the last character !


            char[] charFileName = System.Text.UnicodeEncoding.Unicode.GetChars(fnBytes);


            string fileName = new string(charFileName);


            fileName = fileName.Substring(0, fileName.Length – 1);


 


            // The file is located after the header, which is 24 bytes long


            // plus the length of the filename.


            byte[] fileContents = new byte[attachmentNodeBytes.Length – (24 + fnLength)];


 


            for (int i = 0; i < fileContents.Length; ++i)


            {


                fileContents[i] = attachmentNodeBytes[24 + fnLength + i];


            }


            string SiteURL = “http://myserver/sites/newsite/newdoclib/” + fileName;


            SPWeb site = new SPSite(SiteURL).OpenWeb();


            site.Files.Add(SiteURL, fileContents);


 


        }


 


    }


}


Remember, if you use coding with your InfoPath Form, you need to make it Full Trust, have to Sign it Digitally and while publishing it in a SharePoint Site it should be approved by an Administrator.


Comments (30)

  1. tricia says:

    i know i'm way late…but i was searching for a way to have files that users attach to the infopath form be included in the email that is sent via a Submit button i have on the form.  when it is clicked the completed form is emailed to me.  but i don't get the file they attached.  i have to open the form in sharepoint in ordet to download the attachment.  

  2. Vaibhav Jain says:

    Hi Pranab,

    I'm getting the following error on running the code…

    The Web application at http://punitp120806d:11100/sites/POC/Documents/Test.txt could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

    Though the URL is perfectly valid and i've digitally signed and given full trust. Ive also made it administrator approved template as i'm site collection admin as well as farm admin….

    Please help 🙁

  3. SC says:

    If you find this solution isn't as flexible as you need, try a web handler solution, you can download a demo here: http://blog.mgallen.com/?p=350

    It will work with any InfoPath form and loop through all attachments, no matter where they are in the form structure.

  4. Pam says:

    Thank you – from 2007 to 2010 – this is very helpful!

  5. uday patel says:

    Hi Pranab,

    Thank you very much in advance.

    Its really urgent for us so any help greatly appreciated.

    Question is : How to delete the Infopath Form attachments Once its been copied over to another document library? Can you please provide me any source code as i have tried deleteself() and executeaction method but nothing seems tobe worked on my code.

    Thanks

    Uday Patel

  6. Selva says:

    I had created the VSTA code what you are mentioned above but

    byte[] attachmentNodeBytes = Convert.FromBase64String(opnXN.ToString());

    Here i am getting error like object reference

    pls let me know

    Thanks

    selva

  7. Avijit Das says:

    Hi Pranab,

    I would like to know what happens with the file attachment control after we detach the attachments to a document library. The options are:

    1. Keep it as it is. In this case, anyway the attachments are embedded in the Form’s XML. So, the Form really does not become lightweight.

    2. Replace the attachment control with hyperlink (not sure if this is possible) and then bring back if I am trying to edit the Form.

    Looking ahead for your response.

    Regards,

    Avi

  8. Sunil says:

    I have an infopath form that is web enabled. I have a file attachment control on my form. Is there a limit on the file size that can be uploaded and if so, are there any workarounds ? I know that sharepoint 2007 itself has a 50MB default file size limit, but, that can be changed by going into CA. Anything similar in infopath ?

    thanks for your help.

    Sunil

  9. Harit says:

    Hi

    I executed the code i get the following error can u please say y that happens

    Invalid character in a Base-64 string.

  10. helen says:

    Hi,The code shows me some error message

    System.IndexOutOfRangeException

    Index was outside the bounds of the array.

      at InfoPathTest.FormCode.CTRL3_5_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)

    why its so please help me

Skip to main content