var onlineCredentials = new SharePointOnlineCredentials(ServiceUserName,securePassword);
var context = new ClientContext(clientUrl);
context.Credentials = onlineCredentials;
ar cc = GetAllFiles(sourceDir); //- get the files
int errorLog = 0;
foreach (string path in cc)
    var filepath = "";
    var sub_folder = new FileInfo(path).Directory.FullName;
    filepath = sub_folder.Split(new[] { sourceDir }, StringSplitOptions.None)[1];
    sub_folder = sub_folder.Split(new[] { sourceDir }, StringSplitOptions.None)[1].Replace("\\", "//");

        var folder = CreateFolder(context.Web, listname, sub_folder);//--- Create folder

        Web web = context.Web;
        FileCreationInformation newFile = new FileCreationInformation();
        newFile.Content = System.IO.File.ReadAllBytes(path);

        newFile.Url = filepath + "\\" + Path.GetFileName(path);
        List docs = web.Lists.GetByTitle(listname);
        Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);

    catch (Exception e)
        System.IO.File.AppendAllText(ErrorLogPath, e.Message);


I am trying to upload my local files to SharePoint using c#. If folder is not in Document Libraray , its creating the folders and sub-folders. But, It's not uploading the files.
In Exception, getting the following error:

Microsoft.Sharepoint.Client.ServerException : {File not Found}


  1. User having Full Admin Permissions.
  2. If I Change user permission from Admin To Normal, getting error

    Access is denied

    But folders were created.

  3. In VS,"Platform target:" is "Any CPU".

Create Folder Function:

public static Folder CreateFolder(Web web, string listTitle, string fullFolderPath)
        if (string.IsNullOrEmpty(fullFolderPath))
            throw new ArgumentNullException("fullFolderPath");
        var list = web.Lists.GetByTitle(listTitle);
        return CreateFolderInternal(web, list.RootFolder, fullFolderPath);
private static Folder CreateFolderInternal(Web web, Folder parentFolder, string fullFolderPath)
        var folderUrls = fullFolderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
        string folderUrl = folderUrls[0];
        var curFolder = parentFolder.Folders.Add(folderUrl);

        if (folderUrls.Length > 1)
            var folderPath = string.Join("/", folderUrls, 1, folderUrls.Length - 1);
            return CreateFolderInternal(web, curFolder, folderPath);
        return curFolder;

It's creating Folder, no issue in creating folder. Only i couldn't upload a file.

  • 1
    We need to see your CreateFolder method Commented Jul 18, 2016 at 10:07
  • Did you really think that adding a method that just calls another "secret" method would help? ;) Commented Jul 18, 2016 at 10:18
  • @Praven Try doing a file upload without first creating the folders. Does that work? If not, then your question can become much simpler/easier to answer.
    – jpaugh
    Commented Jan 19, 2018 at 17:42

1 Answer 1


Assuming that you are all sorted with your file paths, I would suggest you to update the list first before updating the listItem.

newFile.Url = filepath + "\\" + Path.GetFileName(path);
List docs = web.Lists.GetByTitle(listname);
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);

context.Load(docs);        //Update the list where the file is being uploaded


That being said, do verify that your paths are coming correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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