从sharepoint文档库中获取所有文档

时间:2012-10-13 11:49:34

标签: c# file sharepoint document

我想从SPDocumentLibrary中检索所有文件我已经尝试过这种方式,但后来我被困了

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);
        }
    }
}

但是SPFileCollection.Add函数需要的不仅仅是文件参数!

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

再一次

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;
         }
     }
 }