Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to retrieve all the existing documents from an SPDocumentLibrary in order to print them so 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 ! so I guess this way don't work , is there any other way to recover my files ?

share|improve this question
    
Glolita, few questions I see some errors in the code like here: SPFileCollection myFiles = myList.; myList.[WHAT]? Also, why are you adding the files in SPFileCollection, and what are you planning to do next with this collection? –  Arsalan Adam Khatri Oct 15 '12 at 9:25
    
as I said I need to print all these found documents and I need to put them in a collection so that I can apply my printing method on them –  Glolita Oct 15 '12 at 9:27
add comment

1 Answer 1

up vote 2 down vote accepted

SPFileCollection is probably a bad idea. I suppose it's too coupled to the internal SharePoint storage behavior.

Replace it with a simple List<SPFile>. This will be easier and works.

A side note, your approach is not taking care of files in subfolders. You should write a small recursive method to get all documents :

private IEnumerable<SPFile> GetAllDocuments() { 
    var myweb = SPContext.Current.Web;
    SPDocumentLibrary myDocLib = (SPDocumentLibrary)myweb.Lists["DocLibrary"];

    return ExploreFolder(myDocLib.RootFolder);


}

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

    }
}
share|improve this answer
1  
@glolita, I've changed my code a bit. I think your site and web variables were messy. Here is a way to execute the code in the current Web. Especially, I did not used the using keyword to avoid disposing context web?§ –  Steve B Oct 15 '12 at 9:30
    
Yeah, you don't need or want to dispose of the current Web object, so removing that was a good tack. I would go so far as to explicitly cast myweb as an SPWeb object because it's easier to work with in Visual Studio but I guess that's in the eye of the beholder... –  NotVonKaiser Sep 6 '13 at 21:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.