Share via


SPFolderCollection Class

Represents a collection of SPFolder objects.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.SPFolderCollection

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class SPFolderCollection _
    Inherits SPBaseCollection

Dim instance As SPFolderCollection
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class SPFolderCollection : SPBaseCollection

Remarks

Use the Folders property of the SPWeb class, or the Subfolders() property of the SPFolder class, to return the collection of folders for a site or folder. To create a folder, use the Add method of SPFolderCollection.

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

Examples

The following code example copies all the subfolders of a Shared Documents document library, excluding the Forms subfolder, into another document library on the same site.

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim site As SPWeb = siteCollection.AllWebs("Site_Name")

Dim srcFolders As SPFolderCollection = 
    site.GetFolder("Shared Documents").SubFolders
Dim destFolder As SPFolder = site.GetFolder("Destination_Folder")

Dim i As Integer

    For i = 0 To srcFolders.Count - 1

        If srcFolders(i).Name <> "Forms" Then

            srcFolders(i).CopyTo(destFolder.Url & "/" & 
                srcFolders(i).Name)

        End If

Next i
SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"])
{
    SPFolderCollection collFolders =
        oWebsite.GetFolder("Shared Documents").SubFolders;
    SPFolder oFolderDest = oWebsite.GetFolder("Destination_Folder");

    for (int intIndex = 0; intIndex < collFolders.Count; intIndex++)
    {
        if (collFolders[intIndex].Name != "Forms")
        {
            collFolders[intIndex].CopyTo(oFolderDest.Url + "/" +
                collFolders[intIndex].Name);
        }
    }
}

Note

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 Best Practices: Using Disposable Windows SharePoint Services Objects.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPFolderCollection Members

Microsoft.SharePoint Namespace