Reputation: 1777
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
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
Reputation: 269
Once Again
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