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)

Syntax

Public ReadOnly Property File As SPFile
    Get

Dim instance As SPListItem
Dim value As SPFile

value = instance.File
public SPFile File { get; }

Property Value

Type: Microsoft.SharePoint.SPFile
An SPFile 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.

Examples

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.

using (SPWeb oWebsite = SPContext.Current.Site.OpenWeb("Website_Name"))
{
    SPList oList = oWebsite.Lists["Shared Documents"];

    string strSearch = "My Value";
    string strQuery = " <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>";

    SPQuery oQuery = new SPQuery();
    oQuery.Query = strQuery;

    SPListItemCollection collItemsRoot = oList.GetItems(oQuery);

    foreach (SPListItem oItemRoot in collItemsRoot)
    {
        if (oItemRoot.FileSystemObjectType == SPFileSystemObjectType.File)
        {
            Response.Write(SPEncode.HtmlEncode(oItemRoot.File.Name) + 
            " == " + oItemRoot.File.CheckOutStatus + "<BR>");
        }
    }

    SPListItemCollection collItemFolders = oList.Folders;

    foreach (SPListItem oItemFolder in collItemFolders)
    {
        oQuery.Folder = oItemFolder.Folder;

        SPListItemCollection collListItems = oList.GetItems(oQuery);

        foreach (SPListItem oListItem in collListItems)
        {
            if (oListItem.FileSystemObjectType == SPFileSystemObjectType.File)
            {
               Response.Write(SPEncode.HtmlEncode(oListItem.File.Name)+ 
                   " == " + oListItem.File.CheckOutStatus + "<BR>");
            }
        }
   }
}

Note

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.

See Also

Reference

SPListItem Class

SPListItem Members

Microsoft.SharePoint Namespace