Tuesday, 6 March 2012

SPFile Class Vs SPList

From : http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfile.aspx

SPFile Class

SharePoint 2010
This topic has not yet been rated Rate this topic
Represents a file in a SharePoint Web site that can be a Web Parts page, an item in a document library, or a file in a folder.
System.Object
  Microsoft.SharePoint.SPFile
Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
[SubsetCallableTypeAttribute]
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.
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 Disposing Objects.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

No comments:

Post a Comment