Joy
Joy

Reputation: 1777

Get all documents from a sharepoint document library

I want to retrieve all documents from an SPDocumentLibrary I've tried this way but then I got stucked

using (SPSite mysite = SPContext.Current.Site)
{
    using (SPWeb myweb = mysite.OpenWeb())
    {
        SPDocumentLibrary myDocLib = (SPDocumentLibrary)myweb.Lists["DocLibrary"];
        SPList myList = SPContext.Current.List;
        SPFileCollection myFiles = myList.;
        foreach (SPListItem myItem in myList.Items)
        {
            //adding each found file to my SPFileCollection
            myFiles.Add(myItem.File);
        }
    }
}

but the SPFileCollection.Add function takes more than the file argument !

Upvotes: 1

Views: 15545

Answers (2)

bmm6o
bmm6o

Reputation: 6515

If I had to guess: you don't actually want to add them to a SPFileCollection. Doing this means that you're copying the files, but without using the convenient Copy method.

You probably just want to store them temporarily in a List<SPFile> or similar.

There are a lot of classes in the SharePoint object library called Collections, but they are not meant to be used like classes in the Systems.Collections namespace.

Upvotes: 1

Waylan Punch
Waylan Punch

Reputation: 269

Once Again

link here

 public static bool getAllDocuments()
 {
     Console.WriteLine("getAllDocuments debug, START");
     bool isOK = false;
     string baseUrl = "http://jabdw3421:82/sites/TestSite/";
     try
     {
         SPSecurity.RunWithElevatedPrivileges(delegate()
         {
             using (SPSite site = new SPSite(baseUrl))
             {
                 using (SPWeb web = site.OpenWeb())
                 {
                     SPDocumentLibrary lib = (SPDocumentLibrary)web.Lists["TestLib"];
                     IEnumerable<SPFile> allFiles = ExploreFolder(lib.RootFolder);

                     foreach (SPFile file in allFiles)
                     {
                         Console.WriteLine("getAllDocuments debug, File Name : " + file.Name);
                         Console.WriteLine("getAllDocuments debug, File CharSetName : " + file.CharSetName);
                         Console.WriteLine("getAllDocuments debug, File SourceLeafName : " + file.SourceLeafName);
                     }
                 }
             }
         });
     }
     catch (Exception e)
     {
         Console.WriteLine("getAllDocuments debug, " + e.Message);
         isOK = false;
     }
     Console.WriteLine("getAllDocuments debug, END");
     return isOK;
 }

 private static IEnumerable<SPFile> ExploreFolder(SPFolder folder)
 {
     foreach (SPFile file in folder.Files)
     {
         yield return file;
     }
     foreach (SPFolder subFolder in folder.SubFolders)
     {
         foreach (SPFile file in ExploreFolder(subFolder))
         {
             yield return file;
         }
     }
 }

Upvotes: 1

Related Questions