SPFolder Class
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

SPFolder Class

Windows SharePoint Services 3

Represents a folder on a SharePoint Web site.


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

[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public class SPFolder

Various folder properties in the Microsoft.SharePoint namespace return a folder object; however, the GetFile method of the SPWeb class returns any folder from within a site or subsite.

Use the Folders property of the SPWeb class, or the SubFolders property of the SPFolder class, to return an SPFolderCollection object that represents the collection of folders for a site or folder. Use an indexer to return a single folder from the collection. For example, if 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.

The following code example displays information about the folders in a site and all its subsites, including the site name, folder name, number of files in the folder, and total size of the files.

This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

The example assumes the existence of an .aspx page that contains a label control.

SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSiteCollection.AllWebs;
foreach (SPWeb oWebsite in collWebsites)
    SPFolderCollection collFolders = oWebsite.Folders;

    foreach (SPFolder oFolder in collFolders)
        SPFileCollection collFiles = oFolder.Files;

        long lngTotalFileSize = 0;

        for (int intIndex = 0; intIndex < collFiles.Count; intIndex++)
            lngTotalFileSize += collFiles[intIndex].Length;

            Label1.Text += " Web: " + 
                + " Folder: " +
                SPEncode.HtmlEncode(oFolder.Name) + " Number: "
                + oFolder.Files.Count +
                " Size: " + lngTotalFileSize + "<BR>";

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.

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