0

I am trying to copy a file from a website to a SharePoint but each time an exception appear:

An unhandled exception of type 'System.Net.WebException' occurred in UploadFileToSharePoint.exe

Additional information: The remote server returned an error: (404) Not Found.

  CopyDocuments("http://www.africau.edu/images/default/sample.pdf", "https://sites.inside-share.name.com/sites/000000", "Documents");


    public void CopyDocuments(string srcUrl, string destUrl, string destLibrary)
    {
        ClientContext srcContext = new ClientContext(srcUrl);

        ClientContext destContext = new ClientContext(destUrl);

        Web srcWeb = srcContext.Web;

        List srcList = srcWeb.Lists.GetByTitle("Documents");

        Web destWeb = destContext.Web;

        destContext.Load(destWeb);

        destContext.ExecuteQuery();
        try
        {
            //"/sites/068295/test.pdf"
            Microsoft.SharePoint.Client.File file = srcContext.Web.GetFileByServerRelativeUrl("http://www.africau.edu/images/default/sample.pdf");
            srcContext.Load(file);
            srcContext.ExecuteQuery();
            string location = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + file.Name;
            FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(srcContext, file.ServerRelativeUrl);
            Microsoft.SharePoint.Client.File.SaveBinaryDirect(destContext, location, fileInfo.Stream, true);
        }

        catch (Exception ex)
        {
            throw ex;
        }
    }
  • Microsoft.SharePoint.Client.File file = srcContext.Web.GetFileByServerRelativeUrl("http://www.africau.edu/images/default/sample.pdf"); this lines implies that the source file is also in SharePoint, which is it not. You need to to a WebRequest or similar to fetch the file from the website before uploading it to SP – Robert Lindgren Mar 13 at 12:52
0

Your code is assuming that the source location (http://www.africau.edu/) is a SharePoint environment, which I feel not. Please follow Robert's suggestion to download the file first before uploading to destination.

I have tried to compile the following code to help you. It might need some small modification. Let me know if it works.

using System.Net;

    public void CopyDocuments(string srcUrl, string destUrl, string destLibrary)
    {
        string downloadedFilePath = @"C:\path\sample.pdf";

        try
        {
            using (var client = new WebClient())
            {
                client.DownloadFile(srcUrl, downloadedFilePath);
            }

            //TODO: Check if downloaded file is present at downloadedFilePath

            ClientContext destContext = new ClientContext(destUrl);

            Web destWeb = destContext.Web;

            destContext.Load(destWeb);

            destContext.ExecuteQuery();

            string location = destWeb.ServerRelativeUrl.TrimEnd('/') 
                + "/" 
                + destLibrary.Replace(" ", "") 
                + "/" 
                + System.IO.Path.GetFileName(filePath);

            using (FileStream fs = new FileStream(downloadedFilePath, FileMode.Open))
            {
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(destContext, location, fs, true);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.