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