SPFile Class

The SPFile class represents a file in a SharePoint Web site that can be a Web Part Page, an item in a document library, or a file in a folder.

System.Object

   Microsoft.SharePoint.SPFile

Public Methods

The following table shows the public methods of the SPFile class and a brief description of each.

Name Description
CheckIn Checks in the file to a document library.
CheckOut Checks out the file from a document library.
CopyTo(String) Copies the file to the destination URL but does not overwrite an existing file of the same name.
CopyTo(String, Boolean) Copies the file to the destination URL but overwrites an existing file of the same name only if bOverwrite is true.
DeleteAllPersonalizations Deletes all personalization information from the Web Part Page for the user with the specified ID.
DeleteAllPersonalizationsAllUsers Deletes all personalization information for all users from the Web Part Page.
GetHtmlTranslateCacheItem Returns the contents of the file from the cache for HTML translation, excluding cache items that are older than the date the file was last modified.
GetHtmlTranslateCacheNameFromStsName Returns the name of the main file from the cache for HTML translation.
GetWebPartCollection Returns the Web Part collection associated with the Web Part Page.
MoveTo(String) Moves the file to the destination URL but does not overwrite an existing file of the same name.
MoveTo(String, Boolean) Moves the file to the destination URL but overwrites an existing file of the same name only if bOverwrite is true.
OpenBinary Opens the file in binary format.
SaveBinary Saves the file in binary format.
SetHtmlTranslateCacheItem Puts the contents of the file into the cache for HTML translation.
StorageManagementWebPartPageInformation Returns information about storage management for the Web Part Page.
ToString Returns the URL for the file based on the site or subsite URL.
UndoCheckOut Undoes the file check-out.

Public Properties

The following table shows the public properties of the SPFile class, the data type of each property, and a brief description of each.

Name Data Type Description
Author Microsoft.SharePoint.SPUser Gets the user who created the document.
CharSetName String Gets the name of the character set used in the file.
CheckedOutBy Microsoft.SharePoint.SPUser Gets the user who has checked out the file.
CheckedOutDate System.DateTime Gets the date and time that the file was checked out.
CheckInComment String Gets the comment used when a document is checked into a document library.
CheckOutExpires System.DateTime Gets the date and time that the check-out expires.
CheckOutStatus Microsoft.SharePoint.SPFile.SPCheckoutStatus Gets the status of the check-out, indicating whether the check-out is long-term or short-term.
Exists Boolean Gets a Boolean value that indicates whether the file exists.
Generator String Gets the name of the application that was used to create the .htm or .aspx file.
IconUrl String Gets the URL to the icon that is associated with the file.
InDocumentLibrary Boolean Gets a Boolean value that indicates whether the file belongs to a document library.
Item Microsoft.SharePoint.SPListItem Gets the list item object that corresponds to the file if the file belongs to a document library.
Length Int64 Gets the size of the file in bytes, including the size of any supporting files, but excluding Web Parts.
LengthByUser Microsoft.SharePoint.SPUserStorageCollection Gets a collection of user storage objects that contain information about each user of the Web Part Page, including the user name, user ID, and the number of bytes that the Web Part personalizations consume.
ModifiedBy Microsoft.SharePoint.SPUser Gets the user object that represents the user who last modified the file.
Name String Gets the internal name of the file.
ParentFolder Microsoft.SharePoint.SPFolder Gets the parent folder of the file.
ProgID String Gets a string that identifies the application in which the file was created.
Properties System.Collections.Hashtable Gets metadata for the file.
ServerRelativeUrl String Gets the relative URL of the file based on the URL for the server.
TimeCreated System.DateTime Gets a date and time value indicating when the file was created.
TimeLastModified System.DateTime Gets a date and time value indicating when the file was last modified.
Title String Gets the display name of the file.
TotalLength Int64 Gets the size of the file in bytes, including the size of any supporting files and Web Parts.
UIVersion Int32 Gets the version number of the file.
Url String Gets the site-relative URL of the file.
Versions Microsoft.SharePoint.SPFileVersionCollection Gets a collection of file version objects representing the versions of the file.

Thread Safety

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

Remarks

Use the GetFile or GetFileAsString method of the SPWeb class to return a single file object. Otherwise, use the Files property of either the SPWeb or SPFolder class to return an SPFileCollection object that represents the collection of files for a site or folder. Use an indexer to return a single file from the collection. For example, if the collection is assigned to a variable named myFiles, use myFiles[index] in C#, or myFiles(index) in Visual Basic .NET, where index is either the index number of the file in the collection or the display name of the file.

Example

The following code example adds a file from the document library of one site to the Shared Documents document library of another site and its subsites.

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

For an example that shows how to upload a local file to a folder on a SharePoint site programmatically, see Uploading a File to a SharePoint Site from a Local Folder.

Requirements

Namespace: Microsoft.SharePoint

Platforms: Microsoft Windows Server 2003

Assembly: Windows SharePoint Services (in Microsoft.SharePoint.dll)

Security: Code Access Security