How to archive infopath forms and attachments in SharePoint by programming

Source: Internet
Author: User

This is the case: We have designed some electronic forms through infopath to carry out some processes for approval within the enterprise. With the forms service provided by Sharepoint Server, we can easily implement it and enter it online, as shown in

The effect of filling in the browser is as follows:

Note: This is just a demo form. I have made several simple fields. The attachment should be highlighted.

 

Now, the requirement is as follows: because these forms are growing and most of them have attachments, the content database of Sharepoint is getting bigger and bigger, and the speed is affected. You can archive the approved forms on a regular basis and delete them from the form library.

When archiving, you will encounter a problem,How to archive attachments and put them in the specified disk folder.

 

First, we need to know how infopath attachments are stored by default? The infopath form is actually a special XML file that stores all information, including attachments, in an XML file. Of course, the attachment will be saved by encoding it into a base64 string. We can save a copy of the data in this form to understand the structure in it.

This file can be opened directly in notepad.

As you can see, the attachment content is saved in this XML file.

Through some research, I have implemented the following solution. This is a prototype and can be used as a reference.

[Note] in this example, we need to carefully store all the data.Demonstrate how to save attachments. Other common data should be easy to process. You can read a table that is stored in the database.

 

The following type is found on the Internet, not my original. This type is a decoder. You can restore the above base64string to a byte array.

   Using System; Using System. Collections. Generic; Using System. text; Using System. IO; /// <Summary>      /// Class used to decode an infopath attachment.      /// Pulls the file name and the decoded file from either a Base 64 byte array or string.      /// </Summary>      Public   Class Infopathattachmentdecoder { // Private string to hold the attachment name.          String _ Filename; // Private byte array to hold the decoded attachment.          Byte [] _ Decodedfile; /// <Summary>          /// The name of the file within the infopath attachment.          /// </Summary>          Public   String Filename {get { Return _ Filename ;}} /// <Summary>          /// The decoded file within the infopath attachment.         /// </Summary>          Public   Byte [] Decodedfile {get { Return _ Decodedfile ;}} /// <Summary>          /// Constructor for the infopathattachmentdecoder class          /// </Summary>          /// <Param name = "base64encodedstring"> the attachment represented by a string </param>          Public Infopathattachmentdecoder ( String Base64encodedstring ){ // Use Unicode encoding. Encoding _ encoding = encoding. Unicode;// The byte array containing the data.              Byte [] _ DATA = convert. frombase64string (base64encodedstring ); // Use a memory stream to access the data.              Using (Memorystream _ memorystream = New Memorystream (_ DATA )){ // Create a binary reader from the stream. Binaryreader _ thereader = New Binaryreader (_ memorystream ); // Create a byte array to hold the header data.                  Byte [] _ Headerdata = _ thereader. readbytes (16 );// Find the file size before finding the file name.                  Int _ Filesize = ( Int ) _ Thereader. readuint32 (); // Get the file name.                  Int _ Attachmentnamelength = ( Int ) _ Thereader. readuint32 () * 2; Byte [] _ Filenamebytes = _ thereader. readbytes (_ attachmentnamelength); _ filename = _ encoding. getstring (_ filenamebytes, 0, _ attachmentnamelength-2 ); // Get the decoded attachment. _ Decodedfile = _ thereader. readbytes (_ filesize );}} /// <Summary>         /// Constructor for the infopathattachmentdecoder class          /// </Summary>          /// <Param name = "base64encodedbytes"> the attachment represented by a byte array </param>          Public Infopathattachmentdecoder ( Byte [] Base64encodedbytes ): This (Convert. tobase64string (base64encodedbytes )){} /// <Summary>          /// Static method that gets the file from the attachment.          /// </Summary>          /// <Param name = "base64encodedstring"> the attachment represented by a string </param>         /// <Returns> Returns a byte array of the file in the attachment. </returns>          Public   Static   Byte [] Decodeinfopathattachment ( String Base64encodedstring ){ // Create an instance of the infopathattachmentdecoder Infopathattachmentdecoder _ infopathattachmentdecoder = New Infopathattachmentdecoder (base64encodedstring ); // Return the decoded file.              Return _ Infopathattachmentdecoder. decodedfile ;} /// <Summary>         /// Static method that gets the file from the attachment.          /// </Summary>          /// <Param name = "base64encodedbytes"> the attachment represented by a byte array </param>          /// <Returns> Returns a byte array of the file in the attachment. </returns>          Public   Static   Byte [] Decodeinfopathattachment ( Byte [] Base64encodedbytes ){ // Create an instance of the infopathattachmentdecoder Infopathattachmentdecoder _ infopathattachmentdecoder = New Infopathattachmentdecoder (base64encodedbytes );// Return the decoded file.              Return _ Infopathattachmentdecoder. decodedfile ;}}

 

Then, we need to read the XML document. Although reading XML files has never been a big problem, the structure of infopath XML documents is still quite tedious. There are a lot of namespaces, and direct reading is quite time-consuming and labor-consuming. I usually use the following method

 

1. Open Visual Studio Command Prompt

2. Generate XSD (schema) based on the XML file)

3. generate a strong Class Based on the XSD File

 

The preparation is complete. Let's make a simpleProgramTo implement the entire archiving logic.

1. Create a Windows Forms Program

[Note] Select. NET Framework 3.5

 

2. Set the compilation platform to x64 (which is required to access the Sharepoint Server Object Model)

 

3. Make a simple interface as follows:

 

4. Reference Microsoft. Sharepoint. dll

 

5. Add the previously generated 10248. CS and infopathattachmentdecoder types to the project.

 

6. Write Code
 Using System;Using System. IO; Using System. LINQ; Using System. Windows. forms; Using System. xml. serialization; Using Microsoft. SharePoint; Namespace Formarchiver { Public   Partial   Class Mainform: FORM { Public Mainform () {initializecomponent ();} Private   Void Btstart_click ( Object Sender, eventargs e) {var Path = txtlibpath. Text; var folder = txtfolder. Text; var site = New Spsite (PATH); var web = site. openweb (); var list = web. getlist (PATH); var items = List. items; Foreach (Splistitem item In Items) {var file = item. file; var stream = file. openbinarystream (); var serializer = New Xmlserializer ( Typeof (Myfields )); // Here, the myfields type is generated by using the XSD tool based on the form structure. VaR result = (myfields) serializer. deserialize (Stream); var attachment = New Infopathattachmentdecoder (result. group3.firstordefault (). field4); var filename = attachment. filename; var buffer = attachment. decodedfile; If (! Directory. exists (folder) directory. createdirectory (folder); var targetpath = path. Combine (folder, filename); file. writeallbytes (targetpath, buffer);} MessageBox. Show ( "Saved" );}}}

The effect of running the program is roughly as follows:

 

Click Start. Soon we can save the attachment to the preset directory.

 

This demo programSource code, Download it here

Formarchiver.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.