1

I need to create an application that exports files from SharePoint (from lists in the site and subsites). I'm planning to create a console application in Visual Studio and use CSOM. I've tried the code solutions in the following links:

Cannot contact site at the specified URL on try ClientContext.ExecuteQuery();

https://stackoverflow.com/questions/17057074/how-to-download-upload-files-from-to-sharepoint-2013-using-csom

but unfortunately, I always get red underlined words in these codes, and I don't know what's wrong or what I'm missing. Can someone help me?

  • Do you import the library to use these classes or methods? – Tarek Salah uddin Mahmud Oct 7 '16 at 8:21
  • Tarek, thanks for you reply. Yes, I had imported the libraries. Nonetheless, after searching all these days, I ended up managing to do what I wanted. – ccga Oct 14 '16 at 15:42
0

Here is the complete code:

class Program
{
    static string url = "SiteURL";
    static string startPath = "Directory to the folder/devise where you want to save";

    static void Main()
    {
        getFiles(url); //saves the files into the desired folder
        Console.WriteLine("\nPress enter to close...");
        Console.Read();
    }

    public static void getFiles(string url)
    {
        try
        {
            ClientContext ctx = new ClientContext(new Uri(@url));

            List List = ctx.Web.Lists.GetByTitle("ListName");
            ctx.Load(List);
            ctx.Load(List.RootFolder);
            ctx.Load(List.RootFolder.Folders);
            ctx.Load(List.RootFolder.Files);
            ctx.ExecuteQuery();

            FolderCollection fcol = List.RootFolder.Folders;
            foreach (Folder f in fcol)
            {
                ctx.Load(f.Files);
                ctx.ExecuteQuery();
                FileCollection fileCol = f.Files;
                foreach (File fileItem in fileCol)
                {
                    //gets the full directory (startpath + directory inside sharepoint)
                    var targetPath = startPath + System.IO.Path.GetDirectoryName(fileItem.ServerRelativeUrl);

                    if (!Directory.Exists(targetPath))
                    {
                        Directory.CreateDirectory(targetPath);
                    }

                    //To download files/items inside list folders
                    CopyFiles(fileItem, targetPath, url); 
                }
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    private static void CopyFiles(File fileItem, string path, string url)
    {
        try
        {
            ClientContext ctx = new ClientContext(new Uri(@url));
            var fileRef = fileItem.ServerRelativeUrl;
            Console.WriteLine(fileRef);
            var fileName = System.IO.Path.GetFileName(fileRef);
            var fileInfo = File.OpenBinaryDirect(ctx, fileRef);
            var filePath = System.IO.Path.Combine(path, fileName);
            var fileStream = System.IO.File.Create(filePath);
            fileInfo.Stream.CopyTo(fileStream);
            fileStream.Close();
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
| improve this answer | |

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.