upload multiple file attachments in InfoPath in Document library with using browser - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

upload multiple file attachments in InfoPath in Document library with using browser

Last post 07-22-2022 09:57 AM by Masoud. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 07-19-2022 12:44 AM

    upload multiple file attachments in InfoPath in Document library with using browser

    Hi

    I have used below code for uploading attachment file from InfoPath to Document library, Now i like to know if I wanted to upload multiple file attachments from InfoPath to document library of sharepoint, what changes I have to do in the form and the code.

    first I try to put attachment field in repeating table of InfoPath but I do not know what other changes I have to do in my form and code.


     

     ---------------------------------------------------------------------------------------------------



    using Microsoft.Office.InfoPath;
    using System;
    using System.Xml;
    using System.Xml.XPath;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using System.Web;

    namespace frmdoc2
    {
        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 InfoPath.
            // It can be modified using Microsoft InfoPath.
            public void InternalStartup()
            {
                ((ButtonEvent)EventManager.ControlEvents["SubmitButton"]).Clicked += new ClickedEventHandler(SubmitButton_Clicked);

            }

            public void SubmitButton_Clicked(object sender, ClickedEventArgs e)
            {
                XPathNavigator docXN = this.CreateNavigator();
                XPathNavigator opnXN = docXN.SelectSingleNode("/my:myFields/my:fileAttach",
                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://www.3isco.local/Detail_eng/" + fileName;
                SPWeb site = new SPSite(SiteURL).OpenWeb();
                site.Files.Add(SiteURL, fileContents);
            }
        }
    }

  • 07-19-2022 04:55 PM In reply to

    Re: upload multiple file attachments in InfoPath in Document library with using browser

     You'll want to put your file attachment field in a repeating group in the schema and on the form. Then, in your code, you need to use docXN.Select with the XPath to the new field, so that you are returned an array of nodes. Then you can use foreach to loop through all of them, and use the current node starting at the declaration for attachmentNodeBytes. Does that make sense?

    Hilary Stoupa

  • 07-20-2022 12:33 PM In reply to

    Re: upload multiple file attachments in InfoPath in Document library with using browser

     Thanks, Hillary

    i have tried to put a file attachment field in the repeating table in the form but actually i do not know what you mean about adding to schema and how?

    also i am new to info path and have read a book about that but i do not know about docXN.Select and ,....

    actually when I see this type of code can read and understand but i can not write, i will be happy if you introduce to me some net address for quick learning this type of code. 

    if it is possible you put what you mean about schema and code in the sample.

     

  • 07-21-2022 09:57 AM In reply to

    Re: upload multiple file attachments in InfoPath in Document library with using browser

    I don't currently have an easily accessible coding environment set up for InfoPath. I've attached a copy of your form with the file attachment field moved into a repeating group. Right click the linked file and save locally, right click the locally saved file and select design to open in design mode.

    Microsoft documentation on XPathNavigator and other things of interest can be found here: https://docs.microsoft.com/en-us/dotnet/api/system.xml.xpath.xpathnavigator?view=net-6.0

    I don't want to discourage you - but InfoPath is deprecated. I'd really think about how much time you want to invest in learning it or learning to write code for it?

    I think the code on your submit button would change to something like this:

     public void SubmitButton_Clicked(object sender, ClickedEventArgs e)
            {
                XPathNavigator docXN = this.CreateNavigator();
                XPathNodeIterator attachments = docXN.Select("/my:myFields/my:Attachments/my:Attachment", this.NamespaceManager);
               
                foreach (XPathNavigator attachment in attachments)
                {
                    XPathNavigator opnXN = attachment.SelectSingleNode("my:fileAttach",
                    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://www.3isco.local/Detail_eng/" + fileName;
                    SPWeb site = new SPSite(SiteURL).OpenWeb();
                    site.Files.Add(SiteURL, fileContents);
                }
               
            }


     

    Hilary Stoupa

  • 07-21-2022 01:28 PM In reply to

    Re: upload multiple file attachments in InfoPath in Document library with using browser

     Thanks Hillary for your guide.

    actually i do not have access to SharePoint online account and i do not have access to power app. It seems i have no way in short time just working with info path because is accessible and still very good forum and support like you.

      Now i am practicing and working with SharePoint server 2013. for me in short time is important learning and understating in action business Analysis of the samples i am working on that. for example uploading automatically from form to SharePoint library was important for me, i am thinking with other software i need do something.

    I will be happy and appreciated if you introduce some Form soft wares  with easy access and good support for using with SharePoint and replacing with info path. 

     

    Regards

     

     

  • 07-21-2022 03:35 PM In reply to

    Re: upload multiple file attachments in InfoPath in Document library with using browser

    I don't have any alternatives to InfoPath to suggest for SharePoint 2013 - I simply wanted to mention that it is deprecated. I'm guessing SharePoint 2013 is also out of support. Microsoft has an Office 365 developer program - they will set up a free Office 365 tenant for developers to test and learn on. I've got one set up and use it all the time for testing - it is super handy: https://developer.microsoft.com/en-us/microsoft-365/dev-program

    Hilary Stoupa

  • 07-22-2022 09:57 AM In reply to

    Re: upload multiple file attachments in InfoPath in Document library with using browser

    Thank you very much Hillary for your guide. I will use free Office 365 tenant for developers to test and learn on.

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