1

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 !

4

2 回答 2

1

如果我不得不猜测:您实际上并不想将它们添加到SPFileCollection. 这样做意味着您正在复制文件,但没有使用方便的 Copy 方法。

您可能只想将它们暂时存储在一个List<SPFile>或类似的地方。

SharePoint 对象库中有很多名为 Collections 的类,但它们并不意味着像 Systems.Collections 命名空间中的类一样使用。

于 2012-10-15T15:42:18.750 回答
1

再次

链接在这里

 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;
         }
     }
 }
于 2016-04-20T07:50:34.090 回答