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

SPListItem.File property

Gets the file that is represented by the item from a document library.

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

'Declaration
Public ReadOnly Property File As SPFile
	Get
'Usage
Dim instance As SPListItem
Dim value As SPFile

value = instance.File

Property value

Type: Microsoft.SharePoint.SPFile
An object that represents the file. Returns a null reference (Nothing in Visual Basic) in a document library if the item does not exist. The File property also returns a null reference (Nothing in Visual Basic) if the item is a folder, or if the item is not located in a document library, although it is not recommended that you call this property in these cases.

The following code example uses the File property to display the file name and check out status of every .xml file in Shared Documents, where the item Title field contains a specified value.

NoteNote

For information about how to use Language-Integrated Query (LINQ) queries to retrieve list items in SharePoint Foundation, see Managing Data with LINQ to SharePoint.

Using oWebsite As SPWeb = SPContext.Current.Site.OpenWeb("Website_Name")
   Dim oList As SPList = oWebsite.Lists("Shared Documents")

   Dim strSearch As String = "My Value"
   Dim strQuery As String = " <Where><And><Contains>" & "<FieldRef Name='Title'/><Value Type='Text'>" & strSearch & "</Value></Contains>" & "<Eq><FieldRef Name='File_x0020_Type'/>" & "<Value Type='Text'>xml</Value></Eq></And></Where>"

   Dim oQuery As New SPQuery()
   oQuery.Query = strQuery

   Dim collItemsRoot As SPListItemCollection = oList.GetItems(oQuery)

   For Each oItemRoot As SPListItem In collItemsRoot
      If oItemRoot.FileSystemObjectType = SPFileSystemObjectType.File Then
         Response.Write(SPEncode.HtmlEncode(oItemRoot.File.Name) & " == " & oItemRoot.File.CheckOutStatus & "<BR>")
      End If
   Next oItemRoot

   Dim collItemFolders As SPListItemCollection = oList.Folders

   For Each oItemFolder As SPListItem In collItemFolders
      oQuery.Folder = oItemFolder.Folder

      Dim collListItems As SPListItemCollection = oList.GetItems(oQuery)

      For Each oListItem As SPListItem In collListItems
         If oListItem.FileSystemObjectType = SPFileSystemObjectType.File Then
            Response.Write(SPEncode.HtmlEncode(oListItem.File.Name) & " == " & oListItem.File.CheckOutStatus & "<BR>")
         End If
      Next oListItem
   Next oItemFolder
End Using
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.

Show:
© 2015 Microsoft