1

I'm just doing some tests for upload files with C# console App but I can't. I've tried everything but with no success, here my code. ¿What do I try? In the List "Contact" there is a sub-folder called "demo" I try to upload the file there in the "demo" folder

       using (SP.ClientContext cnx = new SP.ClientContext("https://test.sharepoint.com/sites/test01"))
        {
            string password = "123";
            string account = "someone@mail.com";
            var secret = new SecureString();
            foreach (char c in password)
            {
                secret.AppendChar(c);
            }
            cnx.Credentials = new SP.SharePointOnlineCredentials(account, secret);

            SP.Web web = cnx.Web;

            SP.FileCreationInformation newFile = new SP.FileCreationInformation();
            newFile.Content = System.IO.File.ReadAllBytes("document.pdf");
            newFile.Url = @"demo/document.pdf";
            SP.List docs = web.Lists.GetByTitle("Contact");
            SP.File uploadFile = docs.RootFolder.Files.Add(newFile);
            cnx.Load(docs);
            cnx.Load(uploadFile);
            cnx.ExecuteQuery();
            Console.WriteLine("done");
        };

what it throws me is the exception Microsoft.SharePoint.Client.ServerException: 'File not found'

I really appreciate your help.

  • What libraries did you call for this project? I'm trying to implement something similar from the same novice in ASP.NET position...... – TrustCode Mar 6 at 21:38
1

You need to get a reference to the folder you want to add to, and add the file directly to that. The new file url should just be the document name

using (SP.ClientContext cnx = new SP.ClientContext("https://test.sharepoint.com/sites/test01"))
    {
        string password = "123";
        string account = "someone@mail.com";
        var secret = new SecureString();
        foreach (char c in password)
        {
            secret.AppendChar(c);
        }
        cnx.Credentials = new SP.SharePointOnlineCredentials(account, secret);

        SP.Web web = cnx.Web;

        SP.FileCreationInformation newFile = new SP.FileCreationInformation();
        newFile.Content = System.IO.File.ReadAllBytes("document.pdf");

        //file url is name
        newFile.Url = @"document.pdf";
        SP.List docs = web.Lists.GetByTitle("Contact");

        //get folder and add to that
        SP.Folder folder = docs.RootFolder.Folders.GetByUrl("demo");
        SP.File uploadFile = folder.Files.Add(newFile);

        cnx.Load(docs);
        cnx.Load(uploadFile);
        cnx.ExecuteQuery();
        Console.WriteLine("done");
    };
  • 1
    I really appreciate your help believe me that I just copy and paste and I stopped to throw me the Exception file not found... below I will post my solution and explain a little bit... Thank you so much @Paul Lucas it is very helpful – PabloDev Jul 27 '18 at 5:11
1

Sample code for your reference:

    string userName = "xxx@xxxx.onmicrosoft.com";
    string password = "xxx";
    var securePassword = new SecureString();
    foreach (char c in password)
    {
        securePassword.AppendChar(c);
    }
    using (var clientContext = new ClientContext("https://testlz.sharepoint.com/sites/jerrydev"))
    {
        clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
        Web web = clientContext.Web;
        clientContext.Load(web, a => a.ServerRelativeUrl);
        clientContext.ExecuteQuery();
        List documentsList = clientContext.Web.Lists.GetByTitle("Contact");

        var fileCreationInformation = new FileCreationInformation();
        //Assign to content byte[] i.e. documentStream

        fileCreationInformation.Content = System.IO.File.ReadAllBytes(@"D:\document.pdf");
        //Allow owerwrite of document

        fileCreationInformation.Overwrite = true;
        //Upload URL

        fileCreationInformation.Url = "https://testlz.sharepoint.com/sites/jerrydev/" + "Contact/demo" + "/document.pdf";

        Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);

        //Update the metadata for a field having name "DocType"
        uploadFile.ListItemAllFields["Title"] = "UploadedviaCSOM";

        uploadFile.ListItemAllFields.Update();
        clientContext.ExecuteQuery();

    }

Make sure the file path is valid in System.IO.File.ReadAllBytes method and the fileCreateionInformation.Url should be the complete library file url not the relative url.

Result:

enter image description here

  • I appreciate your help @Jerry_MSFT people like you are very valuable and dependable in trying to spend a little of your time to solve some questions I really appreciate that keep doing this... But Unfortunately, it keeps me throwing that error about Not Found. I don't know what is happening, but I found the solution and below I will write about it and help some people to face this issue or make'em figure out how to solve it.... thank you so much for your time. regards. – PabloDev Jul 27 '18 at 5:18
0

FOR SHAREPOINT 365 ONLINE .... The answer that was given by @Paul Lucas is right on the spot, I just copy and paste his code and works perfectly, but I would like to share my code and splain a little bit about the code to help someone else to face this kind of issue, what I did is this...

    using (SP.ClientContext cnx = new SP.ClientContext("https://test.sharepoint.com/sites/test01"))
        {
            string password = "xxx";
            string account = "someone@mail.com";
            var secret = new SecureString();
            string docLibrary = "Contact";
            string folderName = "demo";
            string filename = "RockN'Roll.pdf";
            foreach (char c in password)
            {
                secret.AppendChar(c);
            }
            cnx.Credentials = new SP.SharePointOnlineCredentials(account, secret);
            SP.Web web = cnx.Web;
            cnx.Load(web,website=>website.Lists, website=>website.ServerRelativeUrl);
            cnx.ExecuteQuery();
            SP.List list =  web.Lists.GetByTitle(docLibrary);
            cnx.Load(list,i=>i.RootFolder.Folders, i=>i.RootFolder);
            cnx.ExecuteQuery();
            var folderToBindTo = list.RootFolder.Folders;
            var folderToUpload = folderToBindTo.Where(i => i.Name == folderName).First() ;

            SP.FileCreationInformation newFile = new SP.FileCreationInformation();
            newFile.Content = System.IO.File.ReadAllBytes("doc0007.pdf");
            newFile.Url = list.RootFolder.ServerRelativeUrl+ "/" + folderName + "/" + filename;
            newFile.Overwrite = true;

            SP.File uploadFile = folderToUpload.Files.Add(newFile);
            cnx.Load(uploadFile);
            cnx.ExecuteQuery();
            Console.WriteLine("done");
        };

what was happening was that I was not putting the right "PATH" and this was solved with the line

newFile.Url = list.RootFolder.ServerRelativeUrl+ "/" + folderName + "/" + filename;

remember that here it asks you for a relative path, NOT ABSOLUTE so it begins like /sites/yourSITE/folder.... but when you tried to load the relative path I'll throw you an Exception that the Relative folder is not instantiated. to solve that you must load it with a lambda expression to handled that like the line

cnx.Load(list,i=>i.RootFolder.Folders, i=>i.RootFolder);

because it behaves like a LAZY LOAD and only loads and important properties and values, this because we maybe we'll do not need some of them.... to end this topic. if you do not put the right path and initialize the web content it will be throwing you a 'File not found'. I would like to add that in some time I put the right path. but I was getting the error 401 Unauthorized even if I put the credential properties right... Hope it helps and if someone is facing this like I was, well your search finally come to end. I once again thank those peolple that help.

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.