1

The code below, is an attempt to get (download) files from a sharepoint. If I try this on my local version, it works like a charm. I can select all items in the document library.

There are several methods I've tried, and I could post some of them here if you like. I can download corrupt files, but even when the link is wrong. If I try this on the TeamSite in Office 365, I get an exception that my link is wrong. But I'm referring to the same site (instead of localhost/dev/ im referring to http://mysite.com/TeamSite/dev/). Any idea what the difference can be? Does Microsoft block something so external connections aren't allowed?

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

This is the Exception message:

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.
Rob
  • 3,556
  • 2
  • 34
  • 53
  • Microsoft does not support this. Our company was trying to get something similar to this solution too. We had to discontinue the project because the current version of Office365 does not support this problem. – Don Williams Oct 09 '12 at 16:21

1 Answers1

2

You can not connect to sharepoint site, deployed on another computer like this. You should use Client Context

for example:

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

you could find more examples on Client Context here

There is allready an example of file download from sharepoint through clientContext.

Community
  • 1
  • 1
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
  • OK will try this now, it looks similar to several "solutions" I've already tried and I ended up with nothing. Thanks in advance. Going to check it out. – Rob Oct 03 '12 at 12:41
  • This is the probable answer. The C# SharePoint APIs can only contact the local server, never a remote one. – Reacher Gilt Oct 11 '12 at 16:33