Friday, April 29, 2011

"DirectoryNotFoundException" when downloading attachment from InfoPath web form

I got this exact exception like here.   It's annoying that we cannot deploy the InfoPath form to second level site collections, and it really surprised me that the problem is still there after installing CU 2011 Feb.

Luckily it's not too hard to get workaround through coding. And once the bug is fixed by SP1 (hopefully), we can remove the extra part easily, because the changes don't affect the OOTB "file attachment" control.

Below is the solution.
  •  Add two fields in a group, then add an action to initialize the value.



  • Form in design mode.



  • Create a document library to store attached files ("ipAttachment")



  • Add code to form submitting event:

//be careful to keep the file name unique

public void saveAttachments()
{
    string str = string.Empty;
    string url = string.Empty;
    string filename = string.Empty;
    string str4 = string.Empty;
    string str5 = string.Empty;
    string listTitle = "ipAttachment";
    string xpath = string.Empty;
    try
    {
        xpath = "/my:myFields/my:OriginalTaskID";
        str4 = this.MainDataSource.CreateNavigator().SelectSingleNode(xpath, this.NamespaceManager).Value;
        xpath = "/my:myFields/my:AutoId";
        str5 = this.MainDataSource.CreateNavigator().SelectSingleNode(xpath, this.NamespaceManager).Value;
        xpath = "/my:myFields/my:GroupDocumentUrl";
        XPathNodeIterator iterator = this.MainDataSource.CreateNavigator().Select(xpath, this.NamespaceManager);
        while (iterator.MoveNext())
        {
            url = iterator.Current.SelectSingleNode("my:DocumentUrl", this.NamespaceManager).Value;
            if (string.IsNullOrEmpty(url))
            {
                str = iterator.Current.SelectSingleNode("my:DocumentFile", this.NamespaceManager).Value;
                if (!string.IsNullOrEmpty(str))
                {
                    InfoPathAttachmentDecoder decoder = new InfoPathAttachmentDecoder(str);
                    byte[] decodedFile = decoder.DecodedFile;
                    filename = decoder.Filename;
                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                        {
                            web.AllowUnsafeUpdates = true;
                            SPList list = web.Lists.TryGetList(listTitle);
                            string str8 = string.Empty;
                            str8 = string.Format("{0}/{1}_{2}_{3}", new object[] { list.RootFolder.ServerRelativeUrl, str5, str4, filename });
                            SPFile file = list.RootFolder.Files.Add(str8, decodedFile, true);
                            url = site.Url;
                            if (!url.EndsWith("/"))
                            {
                                url = url + "/";
                            }
                            url = url + file.Url;
                            web.AllowUnsafeUpdates = false;
                        }
                    }
                    iterator.Current.SelectSingleNode("my:DocumentUrl", this.NamespaceManager).SetValue(SPEncode.UrlEncodeAsUrl(url));
                    iterator.Current.SelectSingleNode("my:DocumentUrl/@my:DocumentName", this.NamespaceManager).SetValue(filename);
                }
            }
        }
    }
    catch (Exception exception)
    {
        this.sysWriteAppEntry(string.Format("strElementPath={0}, strDocumentName={1}", xpath, filename));
        this.sysWriteAppEntry(string.Format("ex.Message={0}, ex.StackTrace={1}", exception.Message, exception.StackTrace));
        throw;
    }
}

The function "InfoPathAttachmentDecoder()" is from the post below:
https://spdactivities.svn.codeplex.com/svn/DP.Sharepoint.Workflow%20-%20PROD/InfoPath/InfoPathAttachmentDecoder.cs



  • Test.  Attach 2 files with the form.


  • After submitting the form, then open it again


Update (30/06/2011): this issue is fixed by SharePoint 2010 Service Pack 1.

No comments:

Post a Comment