Hope someone could help me figure out this oddity and strange thing happening with the final uploaded serialized xml form file in sharepoint.
Have the need to programatically create infopath form, attach a pdf (binary) file and upload to Sharepoint Form Library (supported by infopath form services).
The Problem:
The file seems to be created correctly as xml after serialization, however when you click to open, it does not open in the browser as it should. Instead requires to
save it locally or open with rich infopath client. (No good for my purposes). Sharepoint shows icon as identifying file as xml, but not as an infopath xml form.
If I serialize without the attachment, then after uploading the file to sharepoint, I am able to open the xml form in form services successfully and the library
identify the xml file correctly as a valid infopath form. However, on the other hand, if I attach the pdf (binary) attachment before I serialize, then serialize, and
proceed to upload to sharepoint, clicking on the file does not open in browser, requiring to either save locally or trying editing with rich infopath client. Is like
it does not gets identified by the library as a valid infopath form, but as plain xml only. As if at the moment of the upload, the "mso-infoPathSolution" and "mso-
application" are not being understood correctly.
And to make matters worst, if I save this file locally, and then try to upload manually to the same library, still does not work. However, the strange part is, if the
same locally saved file is opened in notepad, re-saved, and then manually upload to sharepoint library, IT WORKS FINE and everything is as expected, able to open xml
form in browser and rest.
Seems like a file format issue caused when the encoded attached Base64 is added the the strong type object before it is serialized.
Something else I have noticed is that when the Memory Stream is serialized, and the file is created, it adds some extra blank lines of spaces at the end if the xml
content.
To reconfirm the problem, another approach I have made is trying to upload the serialized form without the pdf attached (binary) to sharepoint library first, just to
follow up by creating another memory stream, encode the attachment, serialize the form and upload to sharepoint under the same name replacing the exiting previous
upload. This seems to work fine, and then I am able to open the xml form in browser and correponding attachment as well. But is very odd, and I can't explain why the
behavior. Of course, I need this to work during the first upload. Can't afford to perform second upload overwritting the previous file, specially if you are dealing
with automatic workflows waiting to kick-off after upload on the library.
Following, an extract of the code being used:
---------------------------------------------
// fileName is existing pdf attachment needed currently file system such as: c:\htaccess.pdf
// I am using MS reference: http://support.microsoft.com/kb/892730 for Decode/Encode file attachements.
InfoPathAttachmentEncoder myEncoder = new InfoPathAttachmentEncoder(fileName);
// Passing encoded attachment to field of strongly typed object instance created using:
// "xsd myschema.xsd /c /l:cs" -> myschema.cs (typed class) represented as "Invoice_Information"
// object in my code, where INV_COPY gererated as "public byte[] INV_COPY" in class.
ObjForm.Invoice_Header_Section.INV_COPY = System.Convert.FromBase64String(myEncoder.ToBase64String());
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.NewLineOnAttributes = true;
xmlWriterSettings.Indent = true;
xmlWriterSettings.OmitXmlDeclaration = false;
MemoryStream stream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(stream, xmlWriterSettings);
writer.WriteProcessingInstruction("mso-infoPathSolution", xInstruction);
writer.WriteProcessingInstruction("mso-application", "progid=\"InfoPath.Document\" versionProgid=\"InfoPath.Document.2\"");
writer.WriteProcessingInstruction("mso-infoPath-file-attachment-present", null);
// Serialize the object Form here...
XmlSerializer serializer = new XmlSerializer(typeof(Invoice_Information));
serializer.Serialize(writer, ObjForm);
using (WebClient client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
client.UploadData(@"http://w2k3dev/sites/mainsite/CABINETS/NYC_CABINET/INV_PENDING_NYC/InvNo3333.xml", "PUT", FAStream.GetBuffer());
}
----------------------------------------------------------------------------------------
Following an extract of the Serialized xml form being uploaded to Sharepoint library:
(Reduced for brevity)
----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:INVOICE-NEWR3:-myXSD-2009-11-05T15-16-11" solutionVersion="1.0.1.648" productVersion="12.0.0.0" PIVersion="1.0.0.0" href="http://w2k3dev/sites/mainsite/FormServerTemplates/INVOICE_NEWR3.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
<?mso-infoPath-file-attachment-present?>
<Invoice_Information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xml:lang="en-US" xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-11-05T15:16:11">
<Invoice_Identifier>AJA12345_ystest4</Invoice_Identifier>
<Invoice_STATUS>IT Review</Invoice_STATUS>
<Invoice_Control>Waiting</Invoice_Control>
<Invoice_Header_Section>
<INV_NUMBER>12345</INV_NUMBER>
<INV_AMT>888.77</INV_AMT>
<INV_VENDOR>TEST_VENDOR</INV_VENDOR>
<INV_VENDORID />
<INV_VENDORTERMS />
<INV_DESC>DESCRIPTION HERE...YS</INV_DESC>
<INV_DATE>2012-02-13</INV_DATE>
<INV_DUE>2012-02-21T00:00:00</INV_DUE>
<INV_COPY>x0lGQRQAAAABAAAAAAAAAJoqAAANAAAAaI.....<..>.... TE2DQolJUVPRg0K</INV_COPY>
<INV_COPY_FILENAME>htaccess.pdf</INV_COPY_FILENAME>
<INV_NOTES />
<INV_MAINTYPE>SERVICES</INV_MAINTYPE>
<INV_SUBTYPE>COMMUNICATIONS</INV_SUBTYPE>
<SOCIETY>NYC</SOCIETY>
<InvCopySecA />
<InvCopySecB />
</Invoice_Header_Section>
<Invoice_PO_Section>
...........
</Invoice_Information>
------------------------------------------------------------------------------------------