Link to home
Start Free TrialLog in
Avatar of bibabutze
bibabutze

asked on

sharepoint document library download script c#

hi experts

I try to dowload files from a sharepoint library and save them to the local file system by using this exe.
Now i have the following error ( see file attached).

how to handle this error.
where do i have to set the property "fGetRootfolder"
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
 
namespace downloadlibrary
{
    class Program
    {
        static string targetf = "";
 
        static void Main(string[] args)
        {
 
            if (args.Length != 3)
            {
                Console.WriteLine("Usage: ShowAllDocuments  <url> <library> <targetpath> ");
                return;
            }
 
            string url = args[0];
            
            string name = args[1];
 
            targetf = args[2];
 
            SPSite site = new SPSite(url);
            SPWeb web = site.OpenWeb();
            
            SPDocumentLibrary mylib= (SPDocumentLibrary)web.Lists[name];
 
            recursiv(mylib.RootFolder);
            
        }
 
        protected static void recursiv(SPFolder folder)
        {   foreach (SPFolder myfolder in folder.SubFolders)
            {
                recursiv(myfolder);
 
                download(myfolder);
                
            }
        }
 
 
        protected static void download(SPFolder folder)
        {   
            foreach (SPFile file in folder.Files)
            {
                string message = "";
                string fileName = file.Name;
                byte[] binfile = file.OpenBinary();
 
                
                bool response = false;
 
                try
                {
                    FileStream fs = new FileStream(targetf + fileName, FileMode.Create, FileAccess.ReadWrite);
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(binfile);
                    bw.Close(); 
                    response = true;
                }
                catch (Exception ex)
                {   Console.WriteLine(ex.Message);
                }
 
                if (response == true)
                {   message =  "Datei: " + fileName + " heruntergeladen ";
                }
                else
                {   message =  "Fehler: " + fileName;
                }
                Console.WriteLine(message);
                
            }
        }
    }
}

Open in new window

shareerror.gif
Avatar of Jamie McAllister
Jamie McAllister
Flag of Switzerland image

I'm not 100% sure where your error is coming from, my German is pretty rusty to be honest (not spoken any for 23 years or so!).

I'm struggling with the intent behind this code. Is it to run on a Sharepoint server and write to that machines local drive or write to some client machine? Is it to be run on the client machine?

I don't see any mention of providing credentials either to get the files, or to write them to the local file system.

I've posted some code that can do individual file downloads from Sharepoint to a local machine (where the code is run from), and I wonder if you could work that into your solution. However I realise that running code on a client machine as this code snippet does may not meet your intent. Please clarify.

Anyway the code snippet is here;
http://www.the-north.com/sharepoint/post/Accessing-Sharepoint-Documents-from-Winforms-via-HTTPS.aspx
btw this is what google tells me that error says;

To the property IncludeRootFolder 'to use, set the property fGetRootFolder' of the SPListCollection object to 'TRUE'
OK. Difficult call without further information. I suspect that the error message is a slight red herring. fGetRootFolder is a private property you can't set (as far as I can tell).

Looking at the code, my best guess would be that myLib wasn't being set up correctly. Is 'P2PlusTest' available in the root web of http://share01/dms ??

If you can provide any further clarification of intent/context etc as mentioned in my first post, that would be great.
ASKER CERTIFIED SOLUTION
Avatar of Jamie McAllister
Jamie McAllister
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bibabutze
bibabutze

ASKER

hi jamie
i had a look on your code. how to get all the content of a document library including subfolder ?

my version: i run this code on a sharepoint machine and want to download all the items within the document library "P2PlusTest" to a local folder on my d:\ harddisk. The documents are *.doc and *.xls files. It doesn´t matter if the programm download aspx files. And i don´t know where i have to set this property.

The purpose of this scipt is to download documents from a doument library that contains more than 100000 files in a single folder.  I get an error if i like to access this folder via unc path (Webclient Service is set on ). As i can find in the internet sharepoint have problems with folders containing more than 2000 files.
So i already tried to use Bamboo Bulk Export (Trial Version) to get my files. First it works fine, but after some tries it throw a sharepoint execption (attached file).

This is why i have to write my own script. I only need to get all this files in my large folder and save them on my local harddisk.




shareerror.gif
I changed my code, the following work. (code attaced)

thanks for your help
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
 
namespace downloadwss2
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {   Console.WriteLine("Usage: ShowAllDocuments z.B: http://share01/dms P2Plus");
                return;
            }
 
            string url = args[0];
            
            string name = args[1];
 
            SPSite site = new SPSite(url);
            SPWeb web = site.OpenWeb();
 
            SPDocumentLibrary doclib = (SPDocumentLibrary)web.Lists[name];
            
            foreach (SPListItem item in doclib.Items)
            {
                SPFile file = item.File;
                byte[] fileBuffer = file.OpenBinary();
                string path = "d:\\import\\" + file.Name;
                FileStream stream = new FileStream(path, FileMode.Create);
                stream.Write(fileBuffer, 0, fileBuffer.Length);
                stream.Close();
                Console.WriteLine("d:\\import\\" + file.Name);
            }
        }
    }
}

Open in new window

You're very welcome bibabutze. That was a fun problem. :)