InfoPath File Attachment Control
File Attachment Control
Applies to: Microsoft Office InfoPath 2003 SP1
Microsoft Office InfoPath 2003 SP1 introduces a number of new controls. One of these new controls, the file attachment control, enables you to insert files which will be encoded and saved with the form. This control opens up a number of new scenarios not easily possible before SP1.
For example, let’s say that you have created a form that will be used by your sales people in the field. Typically, your sales people may track large amount of sales data in an Excel spreadsheet which includes fancy charts and pivot tables. When these sales people report their quarterly earnings, you would like them to fill out an InfoPath form so that you can gather data such as total sales per region. However, since the sales people have already gathered all the detailed sales data in an Excel spreadsheet, you don’t want them to have to reenter the data into the InfoPath form. Instead, your sales people click on a file attachment control and attach the spreadsheet containing their sales data. Later, when you are reviewing the data in the InfoPath form, you can double-click the spreadsheet file and open it in Excel in order to view the detailed sales data.
Adding a file attachment control to a form is easy. Just follow these four basic steps:
Start InfoPath and design a new blank form or design an existing form.
Open the Controls task pane by clicking on the Controls link in the Design Tasks pane.
Look for the File Attachment control under the File and Picture category of the Controls task pane.
Insert the file attachment control by clicking on it or dragging and dropping it into the view.
Now, when you preview the form or fill out a form based on this form template, users can attach files to your form. Also, if you open a form that has existing file attachments, you can double-click on the control to open it. When opening files that are already attached, by default, the application that is associated with that file’s extension on your system is opened in order to view the file.
Security
For security reasons, not all types of files can be attached to a form. The last thing you want is for somebody to attach a malicious executable file that formats your hard drive. For this reason, InfoPath blocks certain file types from being attached (e.g., .exe, .com, .bat, etc.). To determine the types of files that are considered unsafe, InfoPath follows a similar model as Outlook which is documented in http://support.microsoft.com/default.aspx?scid=kb;EN-US;290497.
In addition to the list of file types blocked by default, you can further restrict the types of files you want users to attach to your form. To do so, perform the following:
Follow the steps above to insert a file attachment control into the view.
Double-click on the file attachment control that you just inserted into your form in order to open the properties dialog for the control.
On the Data tab of the properties dialog, check “Allow the user to attach only the following file types” option.
In the textbox below this option, enter the extension or extensions of the file types that you want your users to be allowed to attach to the form. Use semi-colons to separate multiple file extensions (e.g., doc; ppt; xls).
Technical Details
Upon attaching a file, InfoPath first builds a header structure which includes information about the file being attached. The structure consists of the following:
· BYTE[4]: Signature (based on the signature for PNG):
(decimal) 199 73 70 65
(hexadecimal) C7 49 46 41
(ASCII C notation) 307 I F A
The first byte is chosen as a non-ASCII value to reduce the probability that a text file may be misrecognized as a file attachment. The rest identifies the file as an InfoPath File Attachment.
· DWORD: Size of the header
· DWORD: IP Version
· DWORD: dwReserved
· DWORD: File size
· DWORD: Size of file name buffer
· File name buffer: variable size
After building the header structure described above, the header and file data are concatenated and base64 encoded. This base64 data is then stored with the XML data in the form. (More information about base64 encoding can be found here: http://www.w3.org/Protocols/rfc1341/5_Content-Transfer-Encoding.html.)
If you wish to write code to work with InfoPath file attachments, the .NET framework provides a Convert class that includes methods to encode and decode base64 data (e.g., Convert.FromBase64String). These methods can be used to decode existing InfoPath file attachment data or to encode a file for storing in an InfoPath form.
When a file attachment control is inserted into an InfoPath form, the following processing instruction is inserted into the XML template for the form:
<?mso-infoPath-file-attachment-present?>
This processing-instruction is added as a convenience for InfoPath but is not meant to be a security feature. If this processing-instruction is removed, when users fill out the form, all file attachment controls will be disabled.
Comments
Anonymous
March 23, 2004
While creating a tool to convert a SharePoint list to an (InfoPath) SharePoint Form Library, we discovered that needed to write out the custom InfoPath header for converted attachments. Using the information on the InfoPath team blog, we put together the a helper class with a single static method to create the blob needed inside the XML file.
http://tips.serriform.com/CSharpCreateInfoPathAttachment.htmAnonymous
April 06, 2004
Great info, BTW what should the signature content be? Just arbitrary data?
ThanksAnonymous
April 06, 2004
The signature must be the following 4 bytes:
decimal: 199 73 70 65
hexadecimal: C7 49 46 41
ASCII C notation: 307 I F AAnonymous
June 28, 2006
Can you build a role based (or other) security mechanism around the File Attachment Control? I mean for example, that users with certain roles can attach, modify, delete files, others can open them only in read only mode. I think that a possible solution could be to programmatically set the "allow the user to browse, delete and replace files" property.
Thanks for your answer in advance.Anonymous
July 20, 2006
This is nice to attach files to a form, but what can you do with them once there? Is there any way Infopath can extract data from named cells within the spreadsheet or read the custom properties associated with the file? How about getting them out of InfoPath?
Seems like this functionality gets us out of the starting gate, but doesn't provide many (obvious) routes for us to take after that.Anonymous
July 14, 2007
When a form is marked as readonly in InfoPath 2007, a file attachment can't be opened. Any reasoning for this? People generally need to be able to view the form (including attachments) when it's marked readonly.Anonymous
September 12, 2007
I'm using Sharepoint 07 and Infopath 07. When I tried to publish my form to Sharepoint including an attachment control, I get this message: "the selected field cannot be promoted because its data type is not supported: base 64binary". When looking at changing the data type and viewing field/control properties, it doesn't appear that there are any other options for this type of field (attachment). Is this because I am publishing a web browser form as opposed to Infopath? Any suggestions for making an attachment work with my form?Anonymous
September 13, 2007
Remove the field from the promoted fields list in Infopath and then try to publish. My guess is that the form is fine except that you cannot promote a field that is an attachment type.Anonymous
May 12, 2008
Hi all How can i change the default open folder for the file attachment control? When i try to attach a file via citrix throws policy errors since c drive is totally hidden from the users. File Attachment Control looks for My Documnets by default. Is there a way to change to e:temp like that? thanks S.SabaAnonymous
May 12, 2008
Hi sabarathnam, As far as I know, there is not a way to set this; however, the control should open to the last opened directory. ScottAnonymous
May 13, 2008
Scott It opens the last opened directory until the infopath is kept open. If i close infopath and open again, it defaults to My Documents. Thanks for your update. regards S.SabaAnonymous
May 13, 2008
Hi S.Saba, Correct - sorry I was not very clear in my last response. ScottAnonymous
May 20, 2008
in relations to comment posted by jprny : "When a form is marked as readonly in InfoPath 2007, a file attachment can't be opened. Any reasoning for this?... Disabling the attachment also causes the user not being able to view/open the attachment. Enabling the attachment allows a user to remove/change the attachment. Is there a way to disable the attachment from being removed/changed but able to be just viewed ? Manin...Anonymous
January 20, 2009
PingBack from http://www.hilpers.com/868563-infopath-dateianlgen-auswertenAnonymous
January 21, 2009
PingBack from http://www.keyongtech.com/1237099-extracting-embedded-file-attachmentsAnonymous
May 06, 2009
I would like to Know if the sharepoint search indexes the content of the attached file. So i wanna know if i can find it if a search its contents.Anonymous
December 16, 2009
Above, "AngieD" stated: I'm using Sharepoint 07 and Infopath 07. When I tried to publish my form to Sharepoint including an attachment control, I get this message: "the selected field cannot be promoted because its data type is not supported: base 64binary". When looking at changing the data type and viewing field/control properties, it doesn't appear that there are any other options for this type of field (attachment). Is this because I am publishing a web browser form as opposed to Infopath? Any suggestions for making an attachment work with my form? " I have deployed the custom workflow action described here: http://www.deviantpoint.com/post/2009/06/06/Custom-SharePoint-Designer-workflow-for-extracting-InfoPath-attachment-fields.aspx It appears that this is possible if the person who create this workflow action can be believed... how was he able to make this work?Anonymous
March 26, 2010
I also would like to know if the SharePoint search can search the contents of an InfoPath file attachment. If not, will this be fixed in SP 2010?Anonymous
February 09, 2012
XPathNavigator docXN = this.CreateNavigator(); XPathNavigator opnXN = docXN.SelectSingleNode("/my:myFields/my:document", 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 � 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://LIBNAME/" + fileName; SPWeb site = new SPSite(SiteURL).OpenWeb(); SPList list = site.Lists["Office Documets"]; SPListItemCollection colListItems = list.GetItems(); int count = 0; foreach (SPListItem item in colListItems) { site.Files.Add(SiteURL, fileContents); } }Anonymous
November 22, 2013
Is there a way to make the attachment field ready only or disable it for a timeframe?Anonymous
November 23, 2013
Hi Jessica, Yes... just place it inside of a "section" control then you can add conditional formatting to the section. Scott