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.

NameDescription
CheckInChecks in the file to a document library.
CheckOutChecks 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.
DeleteAllPersonalizationsDeletes all personalization information from the Web Part Page for the user with the specified ID.
DeleteAllPersonalizationsAllUsersDeletes all personalization information for all users from the Web Part Page.
GetHtmlTranslateCacheItemReturns 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.
GetHtmlTranslateCacheNameFromStsNameReturns the name of the main file from the cache for HTML translation.
GetWebPartCollectionReturns 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.
OpenBinaryOpens the file in binary format.
SaveBinarySaves the file in binary format.
SetHtmlTranslateCacheItemPuts the contents of the file into the cache for HTML translation.
StorageManagementWebPartPageInformationReturns information about storage management for the Web Part Page.
ToStringReturns the URL for the file based on the site or subsite URL.
UndoCheckOutUndoes 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.

NameData TypeDescription
AuthorMicrosoft.SharePoint.SPUserGets the user who created the document.
CharSetNameStringGets the name of the character set used in the file.
CheckedOutByMicrosoft.SharePoint.SPUserGets the user who has checked out the file.
CheckedOutDateSystem.DateTimeGets the date and time that the file was checked out.
CheckInCommentStringGets the comment used when a document is checked into a document library.
CheckOutExpiresSystem.DateTimeGets the date and time that the check-out expires.
CheckOutStatusMicrosoft.SharePoint.SPFile.SPCheckoutStatusGets the status of the check-out, indicating whether the check-out is long-term or short-term.
ExistsBooleanGets a Boolean value that indicates whether the file exists.
GeneratorStringGets the name of the application that was used to create the .htm or .aspx file.
IconUrlStringGets the URL to the icon that is associated with the file.
InDocumentLibraryBoolean Gets a Boolean value that indicates whether the file belongs to a document library.
ItemMicrosoft.SharePoint.SPListItemGets the list item object that corresponds to the file if the file belongs to a document library.
LengthInt64Gets the size of the file in bytes, including the size of any supporting files, but excluding Web Parts.
LengthByUserMicrosoft.SharePoint.SPUserStorageCollectionGets 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.
ModifiedByMicrosoft.SharePoint.SPUserGets the user object that represents the user who last modified the file.
NameStringGets the internal name of the file.
ParentFolderMicrosoft.SharePoint.SPFolderGets the parent folder of the file.
ProgIDStringGets a string that identifies the application in which the file was created.
PropertiesSystem.Collections.HashtableGets metadata for the file.
ServerRelativeUrlStringGets the relative URL of the file based on the URL for the server.
TimeCreatedSystem.DateTime Gets a date and time value indicating when the file was created.
TimeLastModifiedSystem.DateTimeGets a date and time value indicating when the file was last modified.
TitleStringGets the display name of the file.
TotalLengthInt64Gets the size of the file in bytes, including the size of any supporting files and Web Parts.
UIVersionInt32Gets the version number of the file.
UrlStringGets the site-relative URL of the file.
VersionsMicrosoft.SharePoint.SPFileVersionCollectionGets 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

Page view tracker