Clearing an attachment from an InfoPath form - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Clearing an attachment from an InfoPath form

Last post 12-11-2012 10:43 AM by Plee_agi. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 12-05-2012 02:58 PM

    Clearing an attachment from an InfoPath form

    I am writing a code that would programatically remove any content from an Attachment control.  There is a nice article about this from bizsupportonline >>

    http://www.bizsupportonline.net/blog/2009/10/delete-remove-clear-attachment-infopath-form/

    a. I created an InfoPath 2010 form, with an Attachment control. I applied this code to the form's Loading Event.  I published to SharePoint 2010; and set the form library to 'Open in Browser'

    b. I then added a document and attached a file to the document.

    This solution works, but only when I reopen using "Edit Microsoft InfoPath" 

    When open the form using a browser (Windows 7 Enterprise SP1), I would get a warning message with a correlation id.  The trace log gave me this message:

    "Business logic failed due to an exception. (User: , Form Name: AttachmentDemo, IP: , Request: , Form ID: urn:schemas-microsoft-com:office:infopath:AttachmentDemo:-myXSD-2012-12-05T20-59-41, Type: SchemaValidationException, Exception Message: Schema validation found non-datatype errors.)"

    Is ther something that I should do differently to clear an attachment control in browser mode?

     

     

     

  • 12-11-2012 12:54 AM In reply to

    Re: Clearing an attachment from an InfoPath form

    In browser forms, even if you use attachment.SetValue(""); to clear a field, it will be treated as non-empty when you try to add the nil attribute.  Instead of that line, you can use this, which will work in both the filler and the browser:

                    if (attachment.MoveToFirstChild())
                    {
                        attachment.DeleteSelf();
                    }

    But is there a reason you're using code to clear out attachments?  This can be done with rules just fine.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 12-11-2012 07:46 AM In reply to

    Re: Clearing an attachment from an InfoPath form

     Thanks for your reply. 

    The reason that I am not using rules:  the original posting did not offer such a solution.  You either did it manually or use his script.

    My project requirements are:

    1. Create a form solution published to SharePoint.
    2. Add a feature that allow users to upload documents to a document library on the same form.  No switching pages for end users -- it should be seamless.
    3. Clear the attachment to upload another next document.  Having users to manual remove the exiting content is not acceptable.  (YUP, I am programming something that SharePoint already does well, but in InfoPath)

    I found this solution >> http://blogs.msdn.com/b/infopath/archive/2006/11/28/the-xsi-nil-attribute.aspx  and tested the script.

    1. This solution includes "node.DeleteSelf()" but require that I set a value to the node.
    2. In the example it set a date value to the node.  I guess he was trying to clear a date field. I do not know how to set a value to an attachment; nor understand why I should do so.
    3. When I implemented the code, it **almost** works.  After running the code, I get a validation error (red asterisk) on the attachment.  This would not go away until I attach a new document.
  • 12-11-2012 08:15 AM In reply to

    Re: Clearing an attachment from an InfoPath form

    I see.  The instructions in that new link you've posted are for the opposite of what you're trying to do.  Those instructions show how to remove a nil attribute and then set a field's value, but what you need to do is clear the field's value and add a nil attribute.

    I believe that if you use the code modification I indicated in my last post, you should be all set.  Did you try that?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 12-11-2012 08:54 AM In reply to

    Re: Clearing an attachment from an InfoPath form

     Yes I did try it -- copied the exact script which resulted in the validation error.

    After further experimenting, this solution finally did the trick:

     public void FormEvents_ViewSwitched(object sender, ViewSwitchedEventArgs e)
            {
                
                XPathNavigator root = MainDataSource.CreateNavigator();
                XPathNavigator attachment = root.SelectSingleNode("//my:myFields/my:Attachment", NamespaceManager);
                            
                      
                if (attachment.MoveToAttribute("nil", attachment.LookupNamespace("xsi")))
                {
                    attachment.DeleteSelf();
                }
                attachment.SetValue("");
                
            }

    Thanks for your insight!

     

  • 12-11-2012 09:14 AM In reply to

    Re: Clearing an attachment from an InfoPath form

    Well, that's still not really how it should be.  Are you saying that this causes a validation error?

    public void FormEvents_ViewSwitched(object sender, ViewSwitchedEventArgs e)
     {

     XPathNavigator root = MainDataSource.CreateNavigator();
     XPathNavigator attachment = root.SelectSingleNode("//my:myFields/my:Attachment", NamespaceManager);
                           
                     
      if (!attachment.MoveToAttribute("nil", attachment.LookupNamespace("xsi")))
    {
     if (attachment.MoveToFirstChild())
     {
       attachment.DeleteSelf();
     }
     attachment.CreateAttribute("xsi", "nil",attachment.LookupNamespace("xsi"), "true");
     }

    }

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 12-11-2012 10:30 AM In reply to

    Re: Clearing an attachment from an InfoPath form

     No. The script that I posted does not cause validation error.

    The error occurred when I copied the script from the 2nd link, which dateTime object as a parameter to SetValue. 

    Do I really need to CreateAttribute after DeleteSelf?

  • 12-11-2012 10:43 AM In reply to

    Re: Clearing an attachment from an InfoPath form

     I tired your second code (which has the CreateAttribute line).  This successfully cleared the attachment control -- and it does not leave me with a validation error.

    Thanks for your help!

     

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