lhiapgpeonk lhiapgpeonk - 1 year ago 60
C# Question

Open InfoPath xml from Sharepoint for xml processing

I'm currently working on a project (WinForms, C#, VS2012), where I need to load an InfoPath-XML file from a SharePoint (2010 I think) document library and do some sniffing around for special elements in it.

I am NOT developing on a SharePoint server, since the connection to SharePoint is only a very small portion of what the program actually does. I just plug into some SharePoint lists to pretend I'm querying a database. (I know, SharePoint lists are no database tables in a proper database sense, but that's not the point) The lists contain data vital to my program and via a web service it is quite simple to get the lists contents.

I am NOT using the SharePoint object model (SharePoint*.dll), since I don't have those and can't properly reference them, web services worked fine until now.

After querying a list I know the filename of a file stored in a document library. When I assemble the url for this file and paste it into the browser of my choice, SharePoint displays the file in its GUI as it is displayed inside InfoPath (when I paste the exact same url).

I have a copy of the xml saved locally. This I can open and process as an xml easily. When however I try to open the file directly from the server, my Stream (or FileStream) contains HTML-Code for the SharePoint site showing me my InfoPath document. Here is my code for opening:

string Path = @"awesome-correct-url";
WebRequest request = WebRequest.Create(Path);
request.UseDefaultCredentials = true; //SharePoint needs to know I am allowed to open the file
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream fs = response.GetResponseStream();
StreamReader sr = new StreamReader(fs);
string text = sr.ReadToEnd();

On reviewing what has been written to the console, I find that it is the sites HTML code.

Is there a way to convince SharePoint to give me the contents of the file rather than the nice reviewing site?

Answer Source

Well, it turns out I stumbled upon the solution shortly after posting the question.

Asthis shows, one can use another webservice - the copy.asmx - to download a filefrom sharepoint, which is sufficient for my needs. In case the link gets broken, here's the Code:

string Path = @"awesome-valid-url";
//CopyService is the reference to the webservice
CopyService.Copy cs = new CopyService.Copy();
cs.Credentials = System.Net.CredentialCache.DefaultCredentials;

CopyService.FieldInformation myFieldInfo = new CopyService.FieldInformation();
CopyService.FieldInformation[] myFieldInfoArray = { myFieldInfo };
byte[] myByteArray;
// Call the web service
uint myGetUint = cs.GetItem(Path, out myFieldInfoArray, out myByteArray);

// Convert into Base64 String
string base64String;
base64String = Convert.ToBase64String(myByteArray, 0, myByteArray.Length);

// Convert to binary array
byte[] binaryData = Convert.FromBase64String(base64String);

// Create a temporary file to write the text of the form to
string tempFileName = @"where-to-put-it.xml";

// Write the file to temp folder
FileStream fs = new FileStream(tempFileName, FileMode.CreateNew, FileAccess.ReadWrite);
fs.Write(binaryData, 0, binaryData.Length);

Maybe I can even work around actually saving the file, I have all the bytes I need anyway.