This topic has not yet been rated - Rate this topic

SPFileCollection Class

Represents a collection of SPFile objects.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
[SubsetCallableTypeAttribute]
[ClientCallableTypeAttribute(Name = "FileCollection", ServerTypeId = "{d367b17c-170b-4691-a1e3-8bccf7686ce4}", 
	CollectionChildItemType = typeof(SPFile))]
public class SPFileCollection : SPBaseCollection

Use the Files property of either the SPWeb or SPFolder class to return the collection of files for the site or folder. To create a new file, use one of the Add methods of SPFileCollection.

Use an indexer to return a single file from the collection. For example, assuming the collection is assigned to a variable named collFiles, use collFiles[index] in C#, or collFiles(index) in Visual Basic, where index is either the index number of the file in the collection or the display name of the file.

The following code example checks every file in the top-level folder of a specified document library for the last time that it was modified and, if the time is less than a certain value, copies the file to another document library.

using (SPSite oSiteCollection = new SPSite("http://MySiteCollection"))
{
    SPWeb oSourceWebsite = oSiteCollection.AllWebs["SourceWebSite"];
    SPWeb oDestinationWebsite = oSiteCollection.AllWebs["DestWebSite"];

    SPFolder oFolder = oSourceWebsite.GetFolder("SourceDocLib");
    SPFileCollection collFiles = oDestinationWebsite.GetFolder("DestDocLib").Files;

    foreach (SPFile oFile in oFolder.Files)
    {
        if (oFile.TimeLastModified < Convert.ToDateTime("12/7/2007 12:00:00 AM"))
        {
            string strDestURL = collFiles.Folder.Url + "/" + oFile.Name;
            byte[] binFile = oFile.OpenBinary();

            collFiles.Add(strDestURL, binFile, true);
        }
    }
    oSourceWebsite.Dispose();
    oDestinationWebsite.Dispose();
}
NoteNote

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Security validation note
It appears that, as of the Oct 2011 CU, certain methods on this class are security checked (a good thing... really). To allow an update to the database when calling methods such as Add(...) as the result of a GET request, you'll need to set AllowUnsafeUpdates to true. I was using Add(string, Stream, bool) before updating to Oct 2011 CU and it worked great. Post-Oct 2011 CU update (including Dec 2011 CU), I get the message, "The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again." Temporarily allowing unsafe updates gets the Add(...) working again.
PowerShell Example
I needed this example "The following code example checks every file in the top-level folder of a specified document library for the last time that it was modified and, if the time is less than a certain value, copies the file to another document library." in PowerShell... My example just copies files from one web to another, without any date check. Figured this would get someone started. Thanks! - Check it out here: http://blog.isaacblum.com/2011/10/04/spfilecollection-class-copy-files-to-another-document-library/