以下代码是尝试从sharepoint获取(下载)文件。 如果我在我的本地版本上尝试这个,它就像一个魅力。我可以选择文档库中的所有项目。
我尝试了几种方法,如果你愿意,我可以在这里发布一些方法。我可以下载损坏的文件,但即使链接错误。 如果我在Office 365中的TeamSite上尝试此操作,我会得到一个例外,即我的链接错误。但我指的是同一个站点(而不是localhost / dev / im引用http://mysite.com/TeamSite/dev/)。知道差异可能是什么? Microsoft会阻止某些内容,因此不允许外部连接吗?
private void btnDownload_Click(object sender, EventArgs e)
{
if (comboBox1.Items.Count > 0 && comboBox1.SelectedIndex != -1)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.ShowDialog();
using (SPSite site = new SPSite("http://localhost/dev/"))
{
using (SPWeb web = site.OpenWeb())
{
SPFolder myLibrary = web.Folders["Management"];
foreach (SPFile file in myLibrary.Files)
{
if (file.Name == comboBox1.SelectedItem.ToString())
{
byte[] bytes = file.OpenBinary();
try
{
FileStream fs = new FileStream(dialog.FileName, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
MessageBox.Show("File downloaded to: " + dialog.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
}
else
{
MessageBox.Show("Select file to download");
}
}
这是异常消息:
The Web application at http://www.gtest.nl/TeamSite/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
答案 0 :(得分:2)
您无法连接到像这样部署在另一台计算机上的sharepoint站点。 您应该使用Client Context
例如:
string siteUrl = "http://MyServer/sites/MySiteCollection";
ClientContext clientContext = new ClientContext(siteUrl);
Web oWebsite = clientContext.Web;
ListCollection collList = oWebsite.Lists;
clientContext.Load(collList);
clientContext.ExecuteQuery();
foreach (SP.List oList in collList)
{
Console.WriteLine("Title: {0} Created: {1}", oList.Title, oList.Created.ToString());
}
您可以在客户端上下文here
中找到更多示例已经从sharepoint通过clientContext下载了example个文件。