Domanda

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.

È stato utile?

Soluzione

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

Altri suggerimenti

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

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top