0

I want to upload a local data to my SP. But unfortunately I get an Microsoft.SharePoint.Client.ServerException which is occurred in Microsoft.SharePoint.Client.Runtime.dll

Aditional information: File not found

My code is:

using (var ctx = new ClientContext(SPURL))
        {
            ctx.Credentials = new NetworkCredential(SPUsername, SPPassword, SPDomain);

            var web = ctx.Web;

            //==== upload file to subfolder
            Folder folder = null;
            FileCreationInformation newFile = null; 
            folder = ctx.Web.GetFolderByServerRelativeUrl(SPRelativeURL); // this comes from: FileDirRef

            newFile = new FileCreationInformation();
            newFile.Content = System.IO.File.ReadAllBytes(Source);
            String fileName = Path.GetFileName(Source);
            newFile.Url = SPRelativeURL + fileName;

            newFile.Url = fileName;
            newFile.Overwrite = true;
            Microsoft.SharePoint.Client.File uploadFile = null; 
            uploadFile = folder.Files.Add(newFile);
            ctx.Load(uploadFile); // if you want the file back to read data

            uploadFile.ListItemAllFields["Title"] = fileName;
            uploadFile.ListItemAllFields.Update();


            ctx.ExecuteQuery();              


        }

Can anyone please give me suggestions why my File is not found? Thanks in advance

0

That is because the SharePoint Server cannot read the file from the path you are providing as the SharePoint will be on a different server and the File Path that you are passing is of your local system.

While calling the Document Upload function you need to pass the Bytes Stream of the document to the SharePoint will then will be converted by SharePoint into a file and will upload it then.

Please refer this code sample

if (_FileUpload.HasFile)
{
   byte[] byteArray= _FileUpload.FileBytes;
   MemoryStream stream = new MemoryStream(byteArray);
   string filename = _FileUpload.FileName;
   string result = MyUploadFunction(filename, stream);
}

This will convert your file into a stream. Then in the document upload function use the following code

newFile.ContentStream = stream;

This will upload the file to SharePoint by converting the file stream into original file type with content.

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.