Click to Rate and Give Feedback

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (5)
SPFolder Class (Microsoft.SharePoint)
Represents a folder on a SharePoint Web site.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public Class SPFolder
Visual Basic (Usage)
Dim instance As SPFolder
C#
[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.

Visual Basic
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
C#
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: " + 
                SPEncode.HtmlEncode(oWebsite.Name)
                + " Folder: " +
                SPEncode.HtmlEncode(oFolder.Name) + " Number: "
                + oFolder.Files.Count +
                " Size: " + lngTotalFileSize + "<BR>";
    }
    oWebsite.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 Best Practices: Using Disposable Windows SharePoint Services Objects.

System.Object
  Microsoft.SharePoint.SPFolder
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
How to upload a local File to a SharePoint WebFolder      Ronny Lewandowski   |   Edit   |   Show History

First create a Site-Object

 SPSite _site = new SPSite(" http://Server: Port");

Then create the Web-Object

 SPWeb _web = _site.OpenWeb();

To avoid a Security Exception while calling _folder.Update() later on, allow unsafe updates.

 _web.AlloUnsafeUpdates();

Then create the Folder-Object

 SPFolder _folder = _web.Folders[FolderName];

Read the local Document

 FileStream fStream = File.OpenRead("C:/_dummy/t.txt");
byte[] contents = new byte[fStream.Length];
 fStream.Read(contents, 0, (int)fStream.Length);
 fStream.Close();

Add the local Document

 _folder.Files.Add("Testdoc.doc", contents);
_folder.Update();
Tags What's this?: Add a tag
Flag as ContentBug
How to upload a local File to a SharePoint WebFolder      Alexey Kamenev   |   Edit   |   Show History

Instead of reading file contents into temp buffer, you may use following code (update it as needed)

private void UploadFile(FileInfo file, SPFolder folder) {
using (FileStream stream = file.OpenRead()) {
folder.Files.Add(file.Name, stream, true, Messages.CheckInComment, false);
        folder.Update();
stream.Close();
}
}
									
Tags What's this?: Add a tag
Flag as ContentBug
Probably an error in the Remarks section      Nebelstreif   |   Edit   |   Show History

There must be a typo on the Remarks section, that says: "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."

This was probably about the SPWeb.GetFolder method.

Tags What's this?: spweb (x) Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker