3

I currently have a Desktop application that is able to upload a file to the root directory of my "Documents" Document Library on the Sharepoint Site I am using.

This is done with the following code;

    Dim context As New ClientContext(serviceSiteUrl)
    context.Credentials = GetCredentials()
    Dim web = context.Web
    Dim newFile = new FileCreationInformation
    newFile.Content = IO.File.ReadAllBytes(destination)
    newFile.Url = Path.GetFileName(destination)

    Dim docs = web.Lists.GetByTitle("Documents")
    docs.RootFolder.Files.Add(newFile)
    context.ExecuteQuery()
    Console.WriteLine("File uploaded")
    context.Dispose()

My question is; How am I able to get this application to navigate instead to one of the folders that is currently present within the document library? Currently, the files being uploaded are within the same directory as the folders i'd like them to be within.

1

2 Answers 2

0

Provide the sub folder url in the sample code below and then file will be uploaded to the sub folder instead of the root:

ClientContext context = new ClientContext("http://MySiteUrl");
using (FileStream fs = new FileStream(@"x:\MyFile.docx", FileMode.Open))
{
   Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/MyLibName/MyFolderName/MyFile.docx", fs, true);
}
0

Please note that SaveBinaryDirect() does not require ExecuteQuery() and cannot be overridden.

You are out of lock when Override. And you need to Override ExecuteQuery() to control throttling as explained here: https://docs.microsoft.com/en-us/sharepoint/dev/general-development/how-to-avoid-getting-throttled-or-blocked-in-sharepoint-online

So using SaveBinaryDirect() is only ok if you are not doing mass upload.

Your Answer

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

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