Click to Rate and Give Feedback

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (13)
SPFile Class (Microsoft.SharePoint)
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.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
Public Class SPFile
Visual Basic (Usage)
Dim instance As SPFile
C#
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
public class SPFile

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 collFiles, use collFiles[index] in C#, or collFiles(index) in Visual Basic, where index is the index number of the file in the collection, the file name including extension, or a Web site relative, site collection relative, or absolute URL.

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

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

Visual Basic
Dim siteCollection As SPSite = SPContext.Current.Site
Dim srcSite As SPWeb = siteCollection.AllWebs("Source_Site_Name")
Dim destSites As SPWebCollection = 
    siteCollection.AllWebs("Destination_Site_Name").Webs

Dim srcFile As SPFile = 
    srcSite.GetFile("Source_Folder_Name/Source_File")
Dim fileName As String = srcFile.Name
Dim binFile As Byte() = srcFile.OpenBinary()

Dim destSite As SPWeb

For Each destSite In  destSites

    If destSite.GetFolder("Shared Documents").Exists Then

        Dim destFolder As SPFolder = 
            destSite.GetFolder("Shared Documents")

        destFolder.Files.Add(fileName, binFile, True)

    End If

Next destSite
C#
SPSite oSiteCollection = SPContext.Current.Site;
SPWeb oWebsiteSrc = oSiteCollection.AllWebs["Source_Site_Name"];
SPWebCollection collWebsites = 
    oSiteCollection.AllWebs["Destination_Site_Name"].Webs;

SPFile oFile = oWebsiteSrc.GetFile("Source_Folder_Name/Source_File");
string strFilename = oFile.Name;
byte[] binFile = oFile.OpenBinary();

foreach (SPWeb oWebsite in collWebsites)
{
    if (oWebsite.GetFolder("Shared Documents").Exists)
    {
        SPFolder oFolder = oWebsite.GetFolder("Shared Documents");
        oFolder.Files.Add(strFilename, binFile, true);
    }
    oWebsite.Dispose(); 
}
oWebsiteSrc.Dispose();
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 Best Practices: Using Disposable Windows SharePoint Services Objects.

System.Object
  Microsoft.SharePoint.SPFile
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Microsoft.SharePoint      KDevMan ... Thomas Lee   |   Edit   |   Show History
My development machine is different than the SharePoint server. How do I get access to the Microsoft.Sharepoint.dll? What is the standard procedure for developing with SharePoint on a seperate development machine?

TP: I don't think this is the right place to ask such a question, but I can answer it.

  1. Standard practice is to have a Virtual PC with Windows Server 03 or 08 and WSS/MOSS installed. You can also have your Visual Studio installed on tha VPC.
  2. My personal approach is to have Windows Server 2008 and Visual Studio 2008 directly on my development notebook (requires sufficient memory, CPU) - much easier debugging, developement. Also I have VS installed on test servers and deploy my applications directly from TFS.
[tfl - 22 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or 
the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a
quicker response using the forums than through the Community Content. For specific help about:
Visual Studio  : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public     : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
Publishing Pages      Paul Turner   |   Edit   |   Show History
This class is also available for publishing pages that sit in a pages libraries. It can be used to check-in/publish/approve pages using the publishing feature.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker