Download InfoPath XML

Business Need

I'm needing to download an InfoPath XML document and read the contents into an application for further processing.

Problem

When downloading the <InfoPathFile>.xml form the SharePoint library, I get the generated HTML from InfoPath Services instead of the actual XML.  Having just spent 7 calendar days looking for the solution and trying everything I could think of, all I could get was the InfoPath Services generated HTML.

Solution

Well, I finally ran across a post that someone talked about doing this same thing (actual one of many posts).  However, at the end of the post, he mentioned that you need to include the "?NoRedirect=true" parameter in the URL.  I immediately search MSDN and found exactly one (1) post on this topic.

Anyway, just for anyone else who want the code I used to do this, here it is:

————————————————————– 

Dim request As HttpWebRequest

Dim response As HttpWebResponse = Nothing

Try

     'This is the part that caused me the problem.
     'The NoRedirect=true keeps the InfoPath Services from getting involved

     request = WebRequest.Create(lsURL & "?NoRedirect=true")

     request.Credentials = System.Net.CredentialCache.DefaultCredentials

     request.AllowWriteStreamBuffering = False

     response = request.GetResponse()

     Dim s As Stream = response.GetResponseStream()

     'Write to disk
     'IFile is set in previous not included code.  It is the complete drive/path/filename
     'CreateDirectory will use what it needs from the drive/path/filename
     'CreateDirectory will also not pass an error if the drive/path/filename stuff exists

     Directory.CreateDirectory(lFile.Directory.ToString)

     Dim fs As New FileStream(lsFileName, FileMode.Create)

     Dim read() As Byte

     ReDim read(256)

 

     Dim count As Integer = s.Read(read, 0, read.Length)     Do While count > 0

          fs.Write(read, 0, count)

          count = s.Read(read, 0, read.Length)

     Loop

     'Close everything

     fs.Close()

     s.Close()

     response.Close()

 

     Catch ex As Exception

          MsgBox(ex.Message & vbCrLf & vbCrLf & "Please contact technical support", MsgBoxStyle.Critical, "Download Error")

          Me.Cursor = System.Windows.Forms.Cursors.Default

          Exit Sub

     End Try

———————————————-

Leave a Reply