1

I am trying to make a method that will allow me to use the parameter for folderName to update the path of file location within SharePoint. However, I am having some issues with getting the proper URL structure for the RootFolder + folderName.

Is there a better way of downloading a file that is specific to a folder structure?


Download_SingleDocument()

 public MemoryStream Download_SingleDocument(string fileName, string folderName)
    {
        using (var clientContext = OpenConnectionToSharePoint())
        { 

            var list = clientContext.Web.Lists.GetByTitle(SharePointRoot);

            clientContext.Load(list);
            clientContext.Load(list.RootFolder);
            clientContext.Load(list.RootFolder.Files);
            clientContext.ExecuteQuery();

            var files = list.RootFolder.Files;
            var file = files.First(f => f.Name == fileName);

            var folders = list.RootFolder.Folders;

            var folder = folders.Where(f => f.Name == folderName);

            var  test = Path.Combine(,folderName);

            var fileReference = file.ServerRelativeUrl;
            var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileReference);

            using (var fileStream = new MemoryStream())
            {
                fileInfo.Stream.CopyTo(fileStream);
                return fileStream;
            }
        }

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.