Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SPFolder class

Represents a folder on a SharePoint Web site.

System.Object
  Microsoft.SharePoint.SPFolder

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

'Declaration
Public Class SPFolder
'Usage
Dim instance As SPFolder

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 of its subsites, including the site name, the folder name, the number of files in the folder, and the 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 also requires a predefined .aspx page that contains a label control.

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim sites As SPWebCollection = siteCollection.AllWebs
Dim site As SPWeb

For Each site In  sites

    Dim folders As SPFolderCollection = site.Folders
    Dim folder As SPFolder

    For Each folder In  folders

        Dim files As SPFileCollection = folder.Files
        Dim totalFileSize As Long = 0
        Dim i As Integer

        For i = 0 To files.Count - 1
            totalFileSize += files(i).Length
        Next i

        Label1.Text += " Web: " & SPEncode.HtmlEncode(site.Name) 
            & " Folder: " _
            & SPEncode.HtmlEncode(folder.Name) & " Number: " 
                & folder.Files.Count _
            & " Size: " & totalFileSize & "<BR>"

    Next folder

Next site
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.
Show:
© 2015 Microsoft