SPFileCollection.Item Property (String)
Gets the file object with the specified URL from the collection. In Microsoft Visual C#, this property is an indexer for the SPFileCollection class.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
Syntax
'Declaration
Public ReadOnly Property Item ( _
urlOfFile As String _
) As SPFile
Get
'Usage
Dim instance As SPFileCollection
Dim urlOfFile As String
Dim value As SPFile
value = instance.Item(urlOfFile)
public SPFile this[
string urlOfFile
] { get; }
Parameters
urlOfFile
Type: System.StringThe site-relative URL of the file.
Property Value
Type: Microsoft.SharePoint.SPFile
An SPFile object that represents the file.
Examples
The following code example uses the indexer to return a specified file object and display the title of the file, when the file was last modified, and the name of the user who modified the file.
This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.
The example also assumes the existence of an .aspx page that contains a label control.
Dim webSite As SPWeb = SPContext.Current.Web
Try
Dim srcFile As SPFile = webSite.GetFolder("MyDocLib").Files("MyFile")
Label1.Text = SPEncode.HtmlEncode(srcFile.Title) + " :: " + srcFile.TimeLastModified + " :: " + srcFile.ModifiedBy.Name
Finally
webSite.Dispose()
End Try
SPWeb oWebsite = SPContext.Current.Web;
SPFile oFile = oWebsite.GetFolder("MyDocLib").Files["MyFile"];
Label1.Text = SPEncode.HtmlEncode(oFile.Title) + " :: " +
oFile.TimeLastModified + " :: " + oFile.ModifiedBy.Name;
The next code example uses the indexer to copy a file from a document library in one site to a document library in another site.
Dim siteCollection As New SPSite("http://MySiteCollection")
Try
Dim destFiles As SPFileCollection = siteCollection.AllWebs("DestinWebSite").GetFolder("DestinDocLib").Files
Dim srcFile As SPFile = siteCollection.AllWebs("SourceWebSite").GetFolder("SourceDocLib").Files("MyFile")
Dim copyFile As Byte() = srcFile.OpenBinary()
destFiles.Add(srcFile.Title, copyFile)
Finally
srcSiteCollection.Dispose()
End Try
using (SPSite oSiteCollection = new SPSite("https://localhost"))
{
SPFileCollection collFiles = oSiteCollection.AllWebs["DestWebSite"].GetFolder("DestDocLib").Files;
SPFile oFile = siteCollection.AllWebs["SourceWebSite"].GetFolder("SourceDocLib").Files["MyFile"];
byte[] binCopyFile = oFile.OpenBinary();
collFiles.Add(oFile.Title, strCopyFile);
}
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 Disposing Objects.