我目前正在开发一个项目(WinForms,C#,VS2012),我需要从SharePoint(2010年我认为)文档库中加载InfoPath-XML文件并对其中的特殊元素进行一些嗅探它
我不是在SharePoint服务器上开发,因为与SharePoint的连接只是程序实际执行的一小部分。我只是插入一些SharePoint列表来假装我查询数据库。 (我知道,SharePoint列表没有正确的数据库意义上的数据库表,但这不是重点)列表包含对我的程序至关重要的数据,通过Web服务获取列表内容非常简单。 / p>
我没有使用SharePoint对象模型(SharePoint * .dll),因为我没有这些并且无法正确引用它们,所以Web服务一直运行到现在。
查询列表后,我知道文件库中存储的文件的文件名。当我汇编此文件的URL并将其粘贴到我选择的浏览器中时,SharePoint会在其GUI中显示该文件,因为它显示在InfoPath中(当我粘贴完全相同的URL时)。
我有一份本地保存的xml副本。这个我可以轻松打开并作为xml处理。但是,当我尝试直接从服务器打开文件时,我的Stream(或FileStream)包含SharePoint网站的HTML代码,显示我的InfoPath文档。这是我打开的代码:
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();
Console.WriteLine(text);
在查看已写入控制台的内容时,我发现它是网站的HTML代码。
有没有办法说服SharePoint给我这个文件的内容而不是好的评论网站?
答案 0 :(得分:0)
嗯,事实证明我在发布问题后不久就偶然发现了解决方案。
如this所示,可以使用另一个webservice(copy.asmx)从sharepoint下载文件,这足以满足我的需求。 如果链接被破坏,这是代码:
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);
fs.Close();
也许我甚至可以解决实际保存文件的问题,无论如何我都需要所有字节。