How to programmatically rename an InfoPath file attachment

Filed under: InfoPath 2007

Learn how you can write C# code to programmatically rename a file that has been attached to an InfoPath form.


This question was asked a while ago by one of my readers, but I really never got to provide a full answer at the time due to time constraints. Since I’ve noticed other readers of my blog wanting to do the same thing, I’ve decided to write a quick post about it.


You may have a requirement where after a user has attached a file to an InfoPath form as shown in Figure 1,

InfoPath form with file attachment named OldFileName.xps
Figure 1. InfoPath form with file attachment named OldFileName.xps.

you want to click a button and change the name of the file that was attached to the InfoPath form.

InfoPath form with file attachment named NewFileName.xps
Figure 2. InfoPath form with file attachment named NewFileName.xps after clicking the button.


You can use the InfoPath encoding decoding article published by Microsoft (How to encode and decode a file attachment programmatically by using Visual C# in InfoPath) to help you rename a file attachment.

The InfoPathAttachmentEncoder class in this article expects you to load a file from the file system, but when renaming an existing file attached to an InfoPath form, you won’t be reading the file from disk, but actually from within the form itself.

Therefore, you’ll have to modify the InfoPathAttachmentEncoder class slightly to suit this scenario.


To create the InfoPath form template:

  1. In InfoPath, create a new Blank form template.
  2. Add an InfoPath File Attachment control named document, a Text Box control named filename, and a Button control to the form template.
  3. Add the InfoPathAttachmentEncoder class file to your InfoPath project.
  4. Copy the InfoPathAttachmentDecoder class from the Microsoft article mentioned previously.
  5. Add the following code to the Clicked event handler of the button:

    XPathNavigator root = MainDataSource.CreateNavigator();

    // Retrieve the file name and attachment
    string newFileName = root.SelectSingleNode(
      "//my:filename", NamespaceManager).Value;
    XPathNavigator attachmentNode = root.SelectSingleNode(
      "//my:document", NamespaceManager);
    string base64Attachment = attachmentNode.Value;

    // Decode the InfoPath file attachment
    InfoPathAttachmentDecoder decoder =
      new InfoPathAttachmentDecoder(base64Attachment);
    byte[] fileData = decoder.DecodedAttachment;

    // Encode the InfoPath file attachment with the new file name
    InfoPathAttachmentEncoder encoder =
      new InfoPathAttachmentEncoder(newFileName, fileData);

    // Save the attachment back in the File Attachment control

  6. Save your work, build the form template, and test it.

You should now be able to attach a file to the InfoPath form, enter a new name for the file attachment, and click the button to change the name of the attachment to the new name you specified.

Other interesting file attachment blog posts

How to submit a file attached to an InfoPath form as an attachment to a SharePoint list item
Learn how you can programmatically save a file attachment from an InfoPath form as an attachment to a SharePoint list item when the InfoPath form is submitted to the SharePoint list.

How to submit an InfoPath form to a SharePoint list and attach the form to the list item
Learn how to use the Attachments collection of a SharePoint list item to save the XML of an entire InfoPath form as an attachment to a SharePoint list item.

Updated March 25, 2009:
Use a custom SharePoint workflow to extract a file attachment from an InfoPath form and upload it to a SharePoint document library
This article explains how you can use a custom Visual Studio sequential workflow on a Form Library to extract data from a File Attachment control on an InfoPath form that was submitted to the Form Library and use this data to add a file to a SharePoint Document Library.


Related Posts


Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.