1

下面的代码尝试从共享点获取(下载)文件。如果我在我的本地版本上尝试这个,它就像一个魅力。我可以选择文档库中的所有项目。

我尝试了几种方法,如果您愿意,我可以在这里发布其中的一些。我可以下载损坏的文件,但即使链接错误。如果我在 Office 365 中的 TeamSite 上尝试此操作,我会收到链接错误的异常。但我指的是同一个站点(而不是 localhost/dev/ 我指的是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.
4

1 回答 1

2

您无法连接到共享点站点,像这样部署在另一台计算机上。您应该使用客户端上下文

例如:

    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());
    }

您可以在此处找到有关客户端上下文的更多示例

已经有一个通过clientContext从sharepoint下载文件的例子

于 2012-10-03T12:18:04.953 回答