Getting the file name of an InfoPath attachment

There will be a time, let me tell you :-) that you’ll want or need the file name of the document that one of your users has attached to your InfoPath form.

You’d think that this functionality would come out of the box… but no!

There’s no easy way of doing this, as I’ve had to knock up some simple C# code to go behind my InfoPath forms. See below:

 
    // THIS IS A FUNCTION I HAVE CREATED MANUALLY
    public void RetrieveFilename()
    {
        try
        {
            // TODO: Verify correct XPath for this InfoPath form
            IXMLDOMNode opnXN =
              thisXDocument.DOM.
                 selectSingleNode("/my:myFields/my:attachedDoc");

            byte[] attachmentNodeBytes =
               Convert.FromBase64String(opnXN.nodeTypedValue
                   .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);        

            // TODO: Verify correct XPath for this InfoPath form
            thisXDocument.DOM.
                selectSingleNode("/my:myFields/my:docOSFileName").text
                = fileName;

            /*
            // USE THIS CODE TO UPLOAD TO A SHAREPOINT LIBRARY
            // The file is located after the header, which is 24 bytes
            // 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];
            }

         
            // TODO: Add correct MOSS site URL
            string SiteURL = "http://my/sites/newse/newdoclib/" 
               + fileName;
            SPWeb site = new SPSite(SiteURL).OpenWeb();
            site.Files.Add(SiteURL, fileContents);

            //END OF COMMENTED OUT SECTION
            */

        }
        catch (Exception e)// TODO: Write better error handler
        {
            System.Diagnostics.Debug.WriteLine(e.ToString());
        }
    }

        // THIS FUNCTION IS GENERATED BY INFOPATH
        // The following function handler is created by Microsoft Office. 
        // Do not modify the type or number of arguments.
        [InfoPathEventHandler(MatchPath = "/my:myFields/my:attachedDoc",
           EventType = InfoPathEventType.OnAfterChange)]
        public void attachedDoc_OnAfterChange(DataDOMEvent e)
        {
            // Write your code here to restore the global state.

            if (e.IsUndoRedo)
            {
                // An undo or redo operation has occurred 
                // and the DOM is read-only.
                return;
            }

            // A field change has occurred and the DOM is writable.
            // Write your code here.
            RetrieveFilename();
        }

Thanks should go to Pranab Paul for providing me the initial idea for this code snippet. Also note that you’ll need to add the various using statements for the XPath and XML classes to be available.

SpittingCAML




You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Getting the file name of an InfoPath attachment”

  1. TijsC Says:

    Very useful thanks.

Leave a Reply